我的程序里面Sqlcommand cmd = new Sqlcommand("select * from table1", conn);myGridView.DataSource = cmd.ExecuteReader();myGridView.DataBind();....然后我下面的程序再执行sqlcommand命令时,它就会报错:有打开的与此命令相关联的 DataReader,必须首先将它关闭。请问我要如何关闭 DataRead 啊,我上面的程序并没有产生 SqlDataReader 的实例啊!谢谢!

解决方案 »

  1.   

    和GridView绑定不建议使用ExecuteReader,建议使用ExecuteDataset
      

  2.   

    想做数据控件绑定吗?
     SqlConnection con = "";//数据库连接语句
     string sSQL = "select * from 表";
     SqlDataAdapter adr = new SqlDataAdapter(sSQL, con);
     DataSet Ds=new DataSet();
              
     adr.Fill(Ds);
      

  3.   

    写conn.Close();
    在Sqlcommand cmd = new Sqlcommand("select * from table1", conn);前
      

  4.   

    你前面建了 DataReader 吗?  sqlDataReader dr = cmd.ExecueReader();
     
          .......
             dr.close()
      

  5.   

    conn.Close(); 
    conn.Open;
    在Sqlcommand   cmd   =   new   Sqlcommand("select   *   from   table1",   conn); 
      

  6.   

    用dateaset吧
    SqlDataAdapter da =new SqlDataAdapter("select * from table1", conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "mytable");
            myGridView.DataSource = ds.Tables["mytable"].DefaultView;
            myGridView.DataBind();
      

  7.   

    myGridView.DataSource = cmd.ExecuteReader();
    cmd.ExecuteReader();返回的是DataReader类型啊~
    cmd执行了操作的~
    cmd.dispose()下就可以了~
    你执行完成了,就要把conn关闭,不然会超过连接池最大连接限制。