sproc 使用 RETURN @value 时可以使用 cmd.ExecuteScalar [英] Can you use cmd.ExecuteScalar when the sproc uses RETURN @value

查看:14
本文介绍了sproc 使用 RETURN @value 时可以使用 cmd.ExecuteScalar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你会用吗

int blah = Convert.ToInt32(cmd.ExecuteScalar());

当 sproc 的最后一条语句执行时:

When the sproc's last statement does:

RETURN @value

只有这样,我才能让它工作:

I can only get it to work if it does:

选择@值

另外,这给了我一个对象空异常:

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.

我必须注意,IMO,存储过程的返回值应该简单地表明过程的状态.所有数据都应通过结果集或输出参数返回.

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.

这篇关于sproc 使用 RETURN @value 时可以使用 cmd.ExecuteScalar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆