protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ysconn con1 = new ysconn();
            con1.sqlString = "select * from ys_info where info_id='" + Request.QueryString["info_id"] + "'";
            SqlDataReader rs = con1.recordset();
            int m, j;
            string A;
            string[] tmpA = new string[1];
            if (rs.HasRows)
            {
                rs.Read();
                info_id.Text = rs["info_id"].ToString();
                               
            }
            rs.Close();
            con1.Close();
            con1.sqlString = "select x_yslx from ys_xq where info_id='" + Request.QueryString["info_id"] + "' group by  x_yslx";
            rs = con1.recordset();
            while (rs.Read())
            {
                for (m = 0; m < this.yslx.Items.Count; m++)
                {
                    if (rs[0].ToString() == this.yslx.Items[m].Value)
                    {
                        this.yslx.Items[m].Selected = true;
                        this.yslx.Items[m].Enabled = false;
                    }
                }
              
            }
            rs.Close();
            con1.Close();
            con1.CloseConn();
        }
        else
        {
            int m;            for (m = 0; m < this.yslx.Items.Count; m++)
            {
                if (this.yslx.Items[m].Selected == true)
                {
     
                    ysconn con1 = new ysconn();
                    con1.sqlString = "select x_yslx from ys_xq where info_id='" + Request.QueryString["info_id"] + "' group by  x_yslx";
                    SqlDataReader rs = con1.recordset(); 
                    rs = con1.recordset();
                     while (rs.Read())
                    {
                      }
                    rs.Dispose();
                    con1.CloseConn();
          
                }
                
            }
           
        }
    }始终提示 ysconn.cs    行: 53  错误: 连接未关闭。连接的当前状态为已打开。
  public SqlDataReader recordset()
    {
        SqlCommand mycom = new SqlCommand(_sqlString, mycon);
        mycon.Open(); //53行
        SqlDataReader reader = mycom.ExecuteReader();
        return reader;
        
    }

解决方案 »

  1.   

    好乱的代码啊
                using(SqlDataReader rs = con1.recordset(CommandBehavior.CloseConnection);)
               {
                int m, j;
                string A;
                string[] tmpA = new string[1];
                if (rs.HasRows)
                {
                    rs.Read();
                    info_id.Text = rs["info_id"].ToString();
                                   
                }
                //rs.Close();
                // con1.Close();

                ....
               }
      

  2.   

    SqlDataReader 数据查询期间是不能Close的,一般的做法是读取后放在DataTable或者List集合变量里,然后返回
      

  3.   

    用using(),可以解决你忘掉关连接的问题。推荐百度学一下,基本上大家都在用。
      

  4.   

    using() 是个不错的办法  写个sqlhelper 比较好