procedure [dbo].[Xdd_CheckLogin]
@UserName nvarchar(18),@checkResult int output
as
if exists(select id,UserName,RealName,Email from Crm_User where UserName ='wl85916' )
set @checkResult=1 --通过验证else
set @checkResult=0 --用户不存在 /// <summary>
        /// 执行存储过程返回 System.Data.SqlClient.SqlDataReader,
        /// 在 System.Data.SqlClient.SqlDataReader 对象关闭时,数据库连接自动关闭。      
        /// <param name="paraValues">传递给存储过程的参数值列表。</param>
        /// <returns>包含查询结果的 System.Data.SqlClient.SqlDataReader 对象。</returns>
        /// </summary>
        public SqlDataReader ExecuteDataReader(out object[] output, int outParaNum, params object[] paraValues)
        {
            using (SqlConnection connection = new SqlConnection(Con_String))
            {                SqlCommand command = this.CreateSqlCommand(connection);
                output = new object[outParaNum];//存储过程中返回值的个数
                try
                {
                    this.DeriveParamenters(command);
                    this.AssignParameterValues(command, paraValues);
                    connection.Open();
                    return command.ExecuteReader(CommandBehavior.CloseConnection);
                    //return command.ExecuteReader();
                }
                catch
                {
                    throw new Exception();
                }
            }
        }
数据处理:
Enum_LoginStar state = Enum_LoginStar.Succeed;
            IDataReader sdr = null;
            info = new Info.GetName(String.Empty, string.Empty, string.Empty, string.Empty);
            DbHelper sp = new DbHelper("Xdd_CheckLogin", DbHelper.Con_String);//类的对象
            object[] paraValues = new object[2];
            paraValues[0] = UserName;//从第二个参数开始赋
            object[] output;
            sdr = sp.ExecuteDataReader(out output,3,paraValues);
            switch (Convert.ToInt32(output[0]))
            {
                case 0:
                    state = Wl_Class.Enum_LoginStar.Err_UserNo;
                    break;
            }
            if (sdr.Read())
            {                string Admin_ID = sdr.GetString(2);
                string pwd = sdr.GetString(3);
            }
  if (sdr.Read())
  {
错误:阅读器关闭时尝试调用 Read 无效。 
请问是为什么???

解决方案 »

  1.   

    它的意思是说
    sdr已经Close了,或者打开sdr的连接对象已经Close了,所以无法Read()的
      

  2.   

    我应该如何修改,难道存储过程的结果集不能够给SqlDataReader ,让SqlDataReader 在进行元组的读取吗?  这里难道只能有DataTable 进行数据的绑定,然后再分别拿出吗?
      

  3.   

    你不能把DataReader给return了,因为你的DataReader是在using里创建的,离开using后链接自动会被关闭,你应该在using内部从DataReader中读取数据,而不是把它返回给其他程序读取,要不然你就要把using (SqlConnection connection = new SqlConnection(Con_String))去掉,改为SqlConnection connection = new SqlConnection(Con_String)这样return后connection就不会自动关闭,不过你必须手工调用connection.Close关闭连接,这样也容易导致connection在异常时无法正确关闭,
      

  4.   

    所以最好的方案是保留using (SqlConnection connection = new SqlConnection(Con_String)),然后不返回DataReader,而是在using大括号内把数据全都读出来处理完再返回,还有一个方案就是不在ExecuteDataReader方法内部创建connection ,而是在ExecuteDataReader方法外部创建connection(用using),然后把connection作为参数传给ExecuteDataReader,然后其他部分还和你的程序一样,
      

  5.   

     /// <summary>
            /// 执行存储过程返回 System.Data.SqlClient.SqlDataReader,
            /// 在 System.Data.SqlClient.SqlDataReader 对象关闭时,数据库连接自动关闭。      
            /// <param name="paraValues">传递给存储过程的参数值列表。</param>
            /// <returns>包含查询结果的 System.Data.SqlClient.SqlDataReader 对象。</returns>
            /// </summary>
            public Object[] ExecuteDataReader(out object[] output, int outParaNum, params object[] paraValues)
            {
                object[] strValues = new object[1];
                using (SqlConnection connection = new SqlConnection(Con_String))
                {
                    IDataReader sdr = null;                
                    SqlCommand command = this.CreateSqlCommand(connection);
                    output = new object[outParaNum];//存储过程中返回值的个数
                    try
                    {
                        this.DeriveParamenters(command);
                        this.AssignParameterValues(command, paraValues);
                        connection.Open();
                        sdr = command.ExecuteReader(CommandBehavior.CloseConnection);
                        if (sdr.Read())
                        {
                            strValues = new object[sdr.FieldCount];
                            sdr.GetValues(strValues);
                        }
                        else
                        {
                            strValues[0] = "null";
                        }                }
                    catch
                    {
                        throw new Exception();
                    }
                    finally
                    {
                        sdr.Close();
                        command.Dispose();
                    }
                    return strValues;            }
            }