可以使用DataReader返回out参数

编程入门 行业动态 更新时间:2024-10-25 10:33:51
本文介绍了可以使用DataReader返回out参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用ExecuteReader 我可以返回 DataReader ,但是 out 参数返回0.

Using ExecuteReader I am able to return a DataReader, but the out parameter is returning 0.

使用ExecuteNonQuery 我能够检索 out 参数(具有正确的值),但是 ExecuteNonQuery 不会返回 DataReader .

Using ExecuteNonQuery I am able to retrieve the out parameter (with the correct value) but the ExecuteNonQuery does not return a DataReader.

以下是提供上下文的过程:

Here is the procedure to give context:

CREATE PROCEDURE [dbo].[SelectDays] @dateStart datetime, @dateEnd datetime, @recordCount bigint out AS BEGIN select @recordCount = count(*) from dbo.[Days] where [Date]>=@dateStart and [Date]<=@dateEnd; select [ID],[Name] from dbo.[Days] where [Date]>=@dateStart and [Date]<=@dateEnd; END

是否可以返回 DataReader 和 out 参数,还是应该为每个程序创建两个单独的过程?

Is there a way I could return a DataReader as well as the out parameter, or should I create two separate procedures for each?

Int32 returnValue = 0; Parameters parameters = new Parameters(); parameters.Add(new SqlParameter("@dateStart", dateStart != null ? (object)dateStart : DBNull.Value)); parameters.Add(new SqlParameter("@dateEnd", dateEnd != null ? (object)dateEnd : DBNull.Value)); SqlParameter out_recordCount = new SqlParameter("@recordCount", SqlDbType.BigInt); out_recordCount.Direction = ParameterDirection.InputOutput; out_recordCount.Value = recordCount; parameters.Add(out_recordCount); SqlParameter return_Value = new SqlParameter("@RETURN_VALUE", SqlDbType.Int); return_Value.Direction = ParameterDirection.ReturnValue; parameters.Add(return_Value); dataReader = thismand.ExecuteReader("dbo.SelectDays", CommandType.StoredProcedure, parameters.ToArray()); if(out_recordCount.Value != DBNull.Value) recordCount = Convert.ToInt64(out_recordCount.Value); returnValue = Convert.ToInt32(return_Value.Value); return returnValue;

推荐答案

在返回任何结果集之后,输出参数的值将位于SQLServer的流中(我相信返回值也是如此).这意味着,直到您从DataReader中读取了所有行(或者我相信将其关闭)之后,您才可以看到该值.因此,告诉您结果集中的行数的输出参数几乎没有用.

The value for your output parameter is in the stream from SQLServer AFTER any results sets returned (I believe this is also true of the return value). That means you won't see the value until after you read all the rows from the DataReader (or close it I believe). So an output parameter that tells you the number of rows in the result set is of little use.

但是,下面的代码片段演示了您应该使用的操作顺序:

However, the code fragment below demonstrates the sequence of operations you should be using:

using(SqlConnection connection = new SqlConnection("[your connection string here]")) { connection.Open(); using (SqlCommand command = connection.CreateCommand()) { command.CommandType = CommandType.StoredProcedure; command.CommandText = "dbo.SelectDays"; command.Parameters.AddWithValue("@dateStart", dateStart != null ? (object)dateStart : DBNull.Value); command.Parameters.AddWithValue("@dateEnd", dateEnd != null ? (object)dateEnd : DBNull.Value); SqlParameter out_recordCount = new SqlParameter("@recordCount", SqlDbType.BigInt); out_recordCount.Direction = ParameterDirection.InputOutput; out_recordCount.Value = recordCount; command.Parameters.Add(out_recordCount); SqlParameter return_Value = new SqlParameter("@RETURN_VALUE", SqlDbType.Int); return_Value.Direction = ParameterDirection.ReturnValue; command.Parameters.Add(return_Value); using(SqlDataReader reader = command.ExecuteReader()) { while(reader.Read()) { /* do whatever with result set data here */ } } /* Output and return values are not available until here */ if (out_recordCount.Value != DBNull.Value) recordCount = Convert.ToInt64(out_recordCount.Value); returnValue = Convert.ToInt32(return_Value.Value); return returnValue; } }

更多推荐

可以使用DataReader返回out参数

本文发布于:2023-05-31 10:04:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/389538.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可以使用   参数   DataReader

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!