sql="****" 
string ConnStr = ConfigurationSettings.AppSettings["siteSqlServer"];
            DataSet ds = new DataSet();
            SqlConnection MyConn = new SqlConnection(ConnStr);
            try
            {
                
                if (MyConn.State.ToString() == "Close") MyConn.Open();
                
                string bid_check = "select * from cs_sections where sectionid=" + bid;
                SqlCommand comm = new SqlCommand(bid_check, MyConn);
                SqlDataReader dr = comm.ExecuteReader();
                if (!dr.Read())
                {
                    sql = "&&&&";
                }
                dr.Close();
                
                SqlDataAdapter da = new SqlDataAdapter(sql, MyConn);
                da.Fill(ds, "table");
                GV1.DataSource = ds.Tables[0].DefaultView;
                GV1.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write("<font color=red>错误信息:" + ex.Message + "</font>");
            }
            finally
            {
                MyConn.Close();
            }GV是控件ID

解决方案 »

  1.   

    提示错误:ExecuteReader requires an open and available Connection. The connection's current state is closed我把这个小问题提到这来是因为我感觉到自己肯定看待ADO.NET有误区
      

  2.   

    这是我的关闭的操作
    //关闭数据库
    //成功返回true,否则返回false
    public void Close()
    {
    if( null != dbConn )
    {
    if( ConnectionState.Closed != dbConn.State )
    {
    dbConn.Close();
    }
    dbConn.Dispose();
    }
    dbConn = null;
    }
    你的这句应该是Closed吧
     if (MyConn.State.ToString() == "Close") MyConn.Open();
      

  3.   

    if (MyConn.State.ToString() == "Close")
    错误。去掉这句看看。就算是判断数据库联接状态也不是用tostring()方法的。
      

  4.   

    if (MyConn.State.ToString() == "Close") MyConn.Open();
    改成
     if (MyConn.State.ToString() == "Closed") MyConn.Open();
      

  5.   

    sql="****" 
    string ConnStr = ConfigurationSettings.AppSettings["siteSqlServer"];
                DataSet ds = new DataSet();
                SqlConnection MyConn = new SqlConnection(ConnStr);
                try
                {
                    
                    if (MyConn.State.ToString() == "Close") MyConn.Open();
                                    
                    SqlDataAdapter da = new SqlDataAdapter(sql, MyConn);
                    da.Fill(ds, "table");
                    GV1.DataSource = ds.Tables[0].DefaultView;
                    GV1.DataBind();
                }
                catch (Exception ex)
                {
                    Response.Write("<font color=red>错误信息:" + ex.Message + "</font>");
                }
                finally
                {
                    MyConn.Close();
                }还有我测过这样的正确编译的
      

  6.   

    if (MyConn.State == ConnectionState.Closed)
      

  7.   

    实际连接不仅仅打开和关闭两个状态,可以参考一下ConnectionState枚举。
    if (MyConn.State != ConnectionState.Opened)
      

  8.   


    Ivy_zheng(最后一只恐龙) 正解
      

  9.   

    就是楼上几位所说
    if (MyConn.State.ToString() == "Close") MyConn.Open();
    改成
     if (MyConn.State.ToString() == "Closed") MyConn.Open();