简体   繁体   English

使用ExecuteReader时运行SQL Server存储过程的Ado.net无法检索输出值

[英]Ado.net running SQL Server stored procedure can't retrieve output value when using ExecuteReader

param.Add(new SqlParameter("@Count", SqlDbType.Int) { Direction = ParameterDirection.Output });

using(var dataset = MsSqlHelper.ExecuteDataset(SqlConn.Conn, CommandType.StoredProcedure, "GetList", param.ToArray()))
{
    // Count.Value is not NULL
}

using (SqlDataReader dr = MsSqlHelper.ExecuteReader(SqlConn.Conn, CommandType.StoredProcedure, "GetList", param.ToArray()))
{
    // Count.Value is NULL    
}

There is an output value in my stored procedure. 我的存储过程中有一个输出值。

If I use ExecuteReader , the value of param Count is null. 如果我使用ExecuteReader ,则参数Count值为null。 But if I change method ExecuteReader to ExecuteDataset , then I can get value of Count . 但是,如果我将ExecuteReader方法更改为ExecuteDataset ,则可以获得Count值。

Why is there no value when I use ExecuteReader? 为什么使用ExecuteReader时没有价值?

By the way, nothing has changed in my example except ExecuteDataset / ExecuteReader . 顺便说一下,在我的示例中,除了ExecuteDataset / ExecuteReader之外,没有任何改变。

You should check the output parameter value after the reader has been closed (ie after the using block), or after reading to the end of the data. 关闭阅读器后(即在using块之后),或在读取到数据末尾之后,应检查输出参数值。 See this KB article . 请参阅此知识库文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ado.net中使用输出参数并选择sql server存储过程的查询结果? - How to use output parameter and select query result of a sql server stored procedure in ado.net? 使用ADO.NET连接到SQL Server存储过程 - Connect to SQL Server Stored procedure with ADO.NET 无法使用 ADO.NET 建立到 SQL Server 的连接 - Can't Establish A Connection To SQL Server Using ADO.NET 使用ADO.Net在模型中脚手架调用SQL存储过程 - Scaffolding calling SQL stored procedure in model using ADO.Net SQL Server - 只有通过 ADO.NET 执行存储过程时才会出现“无效的列名”异常 - SQL Server - 'Invalid column name' exception only when executing stored procedure through ADO.NET 存储过程-ADO.NET - Stored procedure - ADO.NET 在 ASP.Net Core 项目中使用 ADO.Net 将 JSON 类型作为参数传递给 SQL Server 2016 存储过程 - Passing JSON type as parameter to SQL Server 2016 stored procedure using ADO.Net in ASP.Net Core project 在 VS2015/C# 中使用存储过程创建 ADO.NET 只读 SQL Server 数据集 - Create an ADO.NET read only SQL Server data set using a stored procedure in VS2015/C# 通过ado.net从C#调用SQL Server存储过程超时 - Calling SQL Server stored procedure from C# via ado.net times out ADO.NET SQL 服务器 select 存储过程可能返回的行数 - ADO.NET number of rows that may be returned by a SQL Server select stored procedure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM