想要实现textbox中的内容随着combobox的改变而改变,代码如下,
为什么会报“连接未关闭,连接当前状态为打开”        private void cmbOBSName_SelectedIndexChanged(object sender, EventArgs e)
        {
            string cOBSName, SQLStr;
            cOBSName = cmbOBSName.Text.Trim().ToString();
            SQLStr = "select cOBSCode from OBSInfo where '" + cmbOBSName + "'";
            DataSet ds = CDataBase.GetDataFromDB(SQLStr);            if (ds != null)
            {
                txtOBSCode.Text = ds.Tables[0].Columns[0].ToString();
            }
        }

解决方案 »

  1.   

    连接未关闭,连接当前状态为打开
    在上一个操作中,没有关闭连接吧(SqlConnection)
      

  2.   

    SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings[""]);//去掉全局static的Connection
    DataSet ds = new DataSet(); 
    SqlDataAdapter da = new SqlDataAdapter("select * from test ", conn); 
    dbcom.conn.Open(); //打开Connection
    da.Fill(ds, "test");
     dbcom.conn.Close();//关闭Connection 
      

  3.   

     数据库连接没关闭吧,要看你CDataBase.GetDataFromDB方法是怎么实现的了
      

  4.   


    ConnectionState 判断一下
      

  5.   

    用 ConnectionState 判断一下con的状态。
    如果打开,关闭行了,在取过数据后 。SqlConnection.Close()
      

  6.   

    SqlConnection 未打开,或者打开了没有关闭,这个要查到当前用的所有读取数据的方法,不光是这个方法引起的,上个方法也有可能
      

  7.   

    这是CDataBase.GetFromDB的方法        public static DataSet GetDataFromDB(string sqlStr)
            {
                conn.Open();//调试时这里报异常
                SqlDataAdapter myAdapter = new SqlDataAdapter(sqlStr, conn);
                DataSet myDataSet = new DataSet();
                myDataSet.Clear();
                myAdapter.Fill(myDataSet);
                conn.Close();
                if (myDataSet.Tables[0].Rows.Count != 0)
                {
                    return myDataSet;
                }
                else
                {
                    return null;
                }
            }
      

  8.   

       conn.Close();
    放最后??conn你的定义是全局的吗?
      

  9.   

    使用 SqlDataAdapter 不需要 conn.Open();在 使用 SqlConnection 对象时 尽量使用 using 语句
      

  10.   

    SqlDataAdapter本身构造函数就有连接参数。
      

  11.   

    单步调试,很可能是connection对象没有关闭释放。
      

  12.   

    conn 就不应该设计为全局成员,应该是在用到地方:new,open,close。
    ADO.NET底层会为你维护一个连接池。
      

  13.   

    你把conn定义成一个公共变量了,肯定是在前一步哪个地方没有关闭,检查代码吧,另外,建议不要定义conn为公共的,而是在查询的最后一步前新建,然后执行操作,然后马上关闭并释放
      

  14.   

    很有可能是你的数据访问类里面的方法没有关闭connection吧