zoukankan      html  css  js  c++  java
  • SqlDataReader 获取存储过程返回值

    编写存储过程,获取不到返回值

    附上代码:

                SqlDataReader reader = null;// 
                totalRecords = 0; 
                try
                {
                    SqlConnectionHolder connection = null;
                    try
                    {
                        connection = SqlConnectionHelper.GetConnection(this._sqlConnectionString, true);
                        SqlCommand command = new SqlCommand("dbo.yourStoredProcedure", connection.Connection);//你的存储过程名称
                        command.CommandType = CommandType.StoredProcedure;
                        command.Parameters.Add(this.CreateInputParam("@pageIndex", SqlDbType.Int, pageIndex));
                        command.Parameters.Add(this.CreateInputParam("@pageSize", SqlDbType.Int, pageSize)); 
    
                        SqlParameter parameter = new SqlParameter("@totalRecords", SqlDbType.Int);//返回值
                        parameter.Direction = ParameterDirection.ReturnValue;
                        command.Parameters.Add(parameter);
                        try
                        {
                            reader = command.ExecuteReader(CommandBehavior.SequentialAccess);
                            while (reader.Read())
                            {
                               data1 = reader.GetGuid(0),
                               data1 = reader.GetInt32(1),                             
                            }
                        } 
                        finally
                        {
                            if (reader != null)
                            {
                                reader.Close();
                            }
                            /*注意:必须关闭reader后才可以读取到返回值*/
                            if ((parameter.Value != null) && (parameter.Value is int)) totalRecords = (int)parameter.Value;
                        }
                        return lists;
                    } 
                    finally
                    {
                        if (connection != null)
                        {
                            connection.Close();
                            connection = null;
                        }
                    }
                }
                catch (Exception ex)
                { 
                    throw;
                }
    View Code

    获取不到返回值原因分析:
    只有SqlDataReader关闭(即调用Close方法),才能对SqlConnection执行其它操作,如获取输出参数的值、返回值和 RecordsAffected。

    详细参考MSDN文档如下:

    1.SqlDataReader创建

    若要创建 SqlDataReader,必须调用 SqlCommand 对象的 ExecuteReader 方法,而不要直接使用构造函数。

    2.SqlDataReader使用

    对于每个关联的 SqlConnection,一次只能打开一个 SqlDataReader,在第一个关闭之前,打开另一个的任何尝试都将失败。类似地,在使用 SqlDataReader 时,关联的 SqlConnection 正忙于为它提供服务,直到调用 Close 时为止。除非调用 SqlDataReader 的 Close 方法,否则会一直处于此状态。例如,在调用 Close 之前,无法检索输出参数。

    SqlDataReader 的用户可能会看到在读取数据时另一进程或线程对结果集所做的更改。但是,确切的行为与执行时间有关。

    当 SqlDataReader 关闭后,只能调用 IsClosed 和 RecordsAffected 属性。尽管当 SqlDataReader 存在时可以访问 RecordsAffected 属性,但是请始终在返回 RecordsAffected 的值之前调用 Close,以保证返回精确的。

    Close 方法填写输出参数的值、返回值和 RecordsAffected,从而增加了关闭用于处理大型或复杂查询的 SqlDataReader 所用的时间。如果返回值和查询影响的记录的数量不重要,则可以在调用 Close 方法前调用关联的 SqlCommand 对象的 Cancel 方法,从而减少关闭 SqlDataReader 所需的时间

    参考文档:来自MSDN http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqldatareader.close(v=vs.85).aspx

  • 相关阅读:
    YbtOJ#573后缀表达【二分图匹配】
    CF605EIntergalaxy Trips【期望dp】
    YbtOJ#482爬上山顶【凸壳,链表】
    AT4996[AGC034F]RNG and XOR【FWT,生成函数】
    YbtOJ#903染色方案【拉格朗日插值,NTT,分治】
    YbtOJ#832鸽子饲养【凸包,Floyd】
    YbtOJ#463序列划分【二分答案,线段树,dp】
    CF618FDouble Knapsack【结论】
    P3214[HNOI2011]卡农【dp】
    YbtOJ#526折纸游戏【二分,hash】
  • 原文地址:https://www.cnblogs.com/lipanpan/p/3380131.html
Copyright © 2011-2022 走看看