使用SqlDataReader时如何获取存储过程的返回值?

发布于 2024-12-15 08:56:27 字数 472 浏览 5 评论 0原文

使用数据读取器时是否无法获取存储过程的返回值?返回值始终为 null,但 SP 从 SSMS 内返回有效的 int。

myCommand.CommandText = "GetVenueVideos";
SqlParameter retVal = new SqlParameter("@returnValue",SqlDbType.Int);
retVal.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(retVal);
myReader = myCommand.ExecuteReader();
if (myReader.Read() && myReader.HasRows)
{
    int returnValue = Convert.ToInt32(retVal.Value);
    //returnValue is null at this point
}

Is it not possible to get the return value of a stored procedeure when using a datareader? The return value is always null, but the SP returns a valid int from within SSMS.

myCommand.CommandText = "GetVenueVideos";
SqlParameter retVal = new SqlParameter("@returnValue",SqlDbType.Int);
retVal.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(retVal);
myReader = myCommand.ExecuteReader();
if (myReader.Read() && myReader.HasRows)
{
    int returnValue = Convert.ToInt32(retVal.Value);
    //returnValue is null at this point
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情独悲 2024-12-22 08:56:27

啊。我发现必须关闭阅读器才能获得返回值!因此,在上面的 if {} 块中,我添加了:

myReader.Close(); // Need to close data reader before getting return value.
int returnValue = (int)myCommand.Parameters["@returnValue"].Value;

...现在它可以正常工作了!

Agh. I've found out the reader has to be closed to get the return value! So inside the if {} block above, I added:

myReader.Close(); // Need to close data reader before getting return value.
int returnValue = (int)myCommand.Parameters["@returnValue"].Value;

...and now it works OK!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文