我有一个返回 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/