您可以使用cmd.ExecuteScalar当存储过程使用RETURN @value [英] Can you use cmd.ExecuteScalar when the sproc uses RETURN @value
问题描述
您可以使用
int blah = Convert.ToInt32(cmd.ExecuteScalar());
在存储过程的最后一条语句的作用:
When the sproc's last statement does:
RETURN @value
我只能得到它的工作,如果它的作用:
I can only get it to work if it does:
选择@value
此外,这给了我一个空对象异常:
Also, this gives me a object null exception:
int blah = (int)cmd.ExecuteScalar();
不convert.toint32和(INT)同样的事情,但一个是另一个的包装?
isn't convert.toint32 and (int) the same thing but one is a wrapper of the other?
推荐答案
没有,你不能。该的ExecuteScalar()方法的目的是返回的结果集返回单值。基本上,在第一行的第一列中的值返回。
No, you cannot. The ExecuteScalar() method is designed to return as single value that is returned in a result set. Basically, the value in the first column of the first row returned.
要得到返回值,则需要一个参数添加到您的SqlCommand对象。使用名称@RETURN_VALUE和创建参数对象时指定返回的参数方向。然后,您可以使用ExecuteNonQuery()方法。
To get the return value, you need to add a parameter to your SQLCommand object. Use the name "@RETURN_VALUE" and specify a parameter direction of Return when creating the parameter object. You can then use the ExecuteNonQuery() method.
我必须指出,国际海事组织,存储过程返回值应该简单地表示该过程的状态。所有的数据应该通过结果集或输出参数返回。
I must note that IMO, stored procedure return values should simply indicate the status of the procedure. All data should be returned through result sets or output parameters.
这篇关于您可以使用cmd.ExecuteScalar当存储过程使用RETURN @value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!