提示错误,已有打开的与此命令相关联的 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();
请问要怎么改啊 ?
谢谢大家
我看到有人说
不要每次都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();
rd.Read();
string filename = rd["FileName"].ToString();
rd.Close(); System.IO.File.Delete("savePath += filename");
cmd.ExecuteNonQuery();
using (SqlDataReader rd = sqlcmd.ExecuteReader())
{
string filename = rd["FileName"].ToString();
}
“System.Data.SqlClient.SqlCommand”是“类型”,但此处被当做“变量”来使用
cmd.CommandText = "语句";