已有打开的与此命令相关联的 DataReader,必须首先将它关闭。
stringopthing_id,eq_ma_no_p,verb_id,state_id1,state_id2,oper_thing,oper_additional,no...
string opthing_id, eq_ma_no_p, verb_id, state_id1, state_id2, oper_thing, oper_additional, noid, opt_id, istemp, oper_time, oper_finished, oper_keyno; ;
string strcon = System.Configuration.ConfigurationManager.AppSettings["con"];
SqlConnection con = new SqlConnection(strcon);
con.Open();
SqlCommand com = new SqlCommand("select * from opts_tcmd ", con);
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
// 输出结果集中的各个字段
opthing_id = dr["opthing_id"].ToString();
eq_ma_no_p = dr["eq_ma_no_p"].ToString();
verb_id = dr["verb_id"].ToString();
state_id1 = dr["state_id1"].ToString();
state_id2 = dr["state_id2"].ToString();
oper_thing = dr["oper_thing"].ToString();
oper_additional = dr["oper_additional"].ToString();
noid = dr["noid"].ToString();
opt_id = dr["opt_id"].ToString();
istemp = dr["istemp"].ToString();
oper_time = dr["oper_time"].ToString();
oper_finished = dr["oper_finished"].ToString();
oper_keyno = dr["oper_keyno"].ToString();
//Response.Write(opthing_id+" ");
string sql = "INSERT INTO opts_taskcmd (opthing_id, eq_ma_no_p, verb_id, state_id1, state_id2, oper_thing, oper_additional, noid, opt_id, istemp, oper_time, oper_finished, oper_keyno) VALUES ('" +
opthing_id + "', '" + eq_ma_no_p + "', '" + verb_id + "', '" + state_id1 + "', '" + state_id2 + "', '" + oper_thing
+ "', " + oper_additional + ", " + noid + ", '" + opt_id + "', " + istemp + ", '" + oper_time + "', " + oper_finished + ", '" + oper_keyno + "')";
SqlCommand com2 = new SqlCommand(sql, con);
com2.ExecuteNonQuery(); 展开
string strcon = System.Configuration.ConfigurationManager.AppSettings["con"];
SqlConnection con = new SqlConnection(strcon);
con.Open();
SqlCommand com = new SqlCommand("select * from opts_tcmd ", con);
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
// 输出结果集中的各个字段
opthing_id = dr["opthing_id"].ToString();
eq_ma_no_p = dr["eq_ma_no_p"].ToString();
verb_id = dr["verb_id"].ToString();
state_id1 = dr["state_id1"].ToString();
state_id2 = dr["state_id2"].ToString();
oper_thing = dr["oper_thing"].ToString();
oper_additional = dr["oper_additional"].ToString();
noid = dr["noid"].ToString();
opt_id = dr["opt_id"].ToString();
istemp = dr["istemp"].ToString();
oper_time = dr["oper_time"].ToString();
oper_finished = dr["oper_finished"].ToString();
oper_keyno = dr["oper_keyno"].ToString();
//Response.Write(opthing_id+" ");
string sql = "INSERT INTO opts_taskcmd (opthing_id, eq_ma_no_p, verb_id, state_id1, state_id2, oper_thing, oper_additional, noid, opt_id, istemp, oper_time, oper_finished, oper_keyno) VALUES ('" +
opthing_id + "', '" + eq_ma_no_p + "', '" + verb_id + "', '" + state_id1 + "', '" + state_id2 + "', '" + oper_thing
+ "', " + oper_additional + ", " + noid + ", '" + opt_id + "', " + istemp + ", '" + oper_time + "', " + oper_finished + ", '" + oper_keyno + "')";
SqlCommand com2 = new SqlCommand(sql, con);
com2.ExecuteNonQuery(); 展开
2个回答
展开全部
DataReader 在打开使用时,一个 SqlConnection 只允许打开一个 DataReader,在开启另一个 DataReader 时,必须将前面一个先 Close 掉。
1、使用数据库更新或插入语句时,通常用 SqlCommand 的 ExecuteNonQuery() 方法,并且定义了一个公共的数据库连接,每次查询时都生成新连接不会有这个问题,但会占用高的数据库资源。
2、这种情况下,内部会生成一个空的 DataReader 对象,当前的数据库连接关闭掉后,该 DataReader 才会释放,因此在推荐使用 using 关键字,可以将它所定义范围内的对象都释放掉。
3、也可以设定ConnectionString,直接加上MultipleActiveResultSets=true语句,只适用于SQL 2005之后的版本,先读出放置在List中。
4、改为下图代码之后,ToList之后就被读到内存中了,与DataContext脱钩了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询