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

附上代码:

            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

相关文章: