提示错误,已有打开的与此命令相关联的 DataReader,必须首先将它关闭。
请问要怎么改啊 ?
谢谢大家
我看到有人说
 不要每次都new一个SqlCommand     
  只要new一次   
  接下来改变它的CommandText就可以了 
怎么改啊 ?
还有人说
using   (SqlCommand   c   =   new   SqlCommand())   
  {   
          c.Execute....();   
  }   
 用这个方法,我也不知道怎么用?
  String savePath = Server.MapPath("UpFile/"); 
        string str = System.Configuration.ConfigurationManager.AppSettings["strconn"];
        SqlConnection conn = new SqlConnection(str);
        conn.Open();
        string DelId = this.GrdPic.DataKeys[e.RowIndex][0].ToString();
        string delsql = "Delete from CustomerInfo WHERE ID = " + DelId;
        string sqlfile = "select FileName from CustomerInfo where Id=" + DelId;        SqlCommand cmd = new SqlCommand(delsql, conn);
        SqlCommand sqlcmd = new SqlCommand(sqlfile, conn);        SqlDataReader rd = sqlcmd.ExecuteReader();
        rd.Read();
        string filename = rd["FileName"].ToString();        System.IO.File.Delete("savePath += filename");
        cmd.ExecuteNonQuery();

解决方案 »

  1.   

    SqlDataReader rd = sqlcmd.ExecuteReader();
            rd.Read();
            string filename = rd["FileName"].ToString();
            rd.Close();        System.IO.File.Delete("savePath += filename");
            cmd.ExecuteNonQuery();
      

  2.   

    用完rd.Read()之后记得将其关闭,语句为rd.Close()
      

  3.   


                using (SqlDataReader rd = sqlcmd.ExecuteReader())
                {
    string filename = rd["FileName"].ToString();
                }
      

  4.   

    用你的方法不行
    “System.Data.SqlClient.SqlCommand”是“类型”,但此处被当做“变量”来使用
      

  5.   

    SqlDataReader是独占式的打开方式,在一个进程当中,只允许打开一个,如果在未关闭之前,任何其他的涉及数据库连接的操作,都会提示错误的,当然了数据集本身的访问是没有问题的。
      

  6.   

    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "语句";