c# - C#调用存储过程获取返回值和记录集

标签 c# sql-server-2008 stored-procedures ado.net

我有一个返回 2 个输出参数和一条记录的存储过程。

但是在 C# 中:

  • ExecuteReader 不允许返回值但允许记录
  • ExecuteNonQuery 允许返回值但不允许记录。

我怎样才能同时获得两者?

最佳答案

在关闭 DataReader 之前,存储过程返回的输出参数不可用。

假设你有

 SqlDataReader reader = cmd.ExecuteReader();
  ...... do you record reading
 reader.Close();

 // Now the output parameters are available
 int result = (int)cmd.Parameters["OutputParameter1"].Value;         

当然这是假设你已经正确设置了你的输出参数....

这是来自 SqlDataReader docs on MSDN

While the SqlDataReader is being used, the associated SqlConnection is busy serving the SqlDataReader, and no other operations can be performed on the SqlConnection other than closing it. This is the case until the Close method of the SqlDataReader is called. For example, you cannot retrieve output parameters until after you call Close.

关于c# - C#调用存储过程获取返回值和记录集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20529799/

相关文章:

javascript - 带有 React 模板的 ASP.NET Core 返回 index.html

sql - 如何从批处理文件运行 .sql 文件?

sql - 在自引用表 SQL Server 上查找最高祖 parent

c# - 如何在 Android 的 Unity 中将 System.IO.Stream 转换为纹理?

c# - .NET 视频播放库允许更改播放速率?

c# - 简单注入(inject)器和 SolrNet

sql - 无法在 EXEC() 后获得 @@ERROR 并出现错误

sql - 为什么稀疏列中的非空值在 SQL Server 2008 中占用额外空间

java - 使用 Spring Data JPA 运行存储过程时出现问题 - SQLServerException : The column name id is not valid

sql-server-2008 - 创建存储过程以修改行(如果存在),如果不存在则将新行插入表中