菜鸟问题,数据库连接
private void timerReceive_Tick(object sender, System.EventArgs e)
{
string mobile_no ;
string sp_id;
string msg;
string receive_time;
timerReceive.Enabled=false;
SqlCommand SqlcomReceiveBuff=new SqlCommand("select * from receivebuff",sqlConnPHS_SMS);
SqlDataReader DrReceiveBuff=SqlcomReceiveBuff.ExecuteReader();
while (DrReceiveBuff.Read())
{
mobile_no= DrReceiveBuff.GetValue(0).ToString();
sp_id= DrReceiveBuff.GetValue(1).ToString();
msg=DrReceiveBuff.GetValue(2).ToString();
receive_time=DrReceiveBuff.GetValue(3).ToString();
if (msg.ToUpper()=="LT")
{
SqlCommand SqlInsertUserChat=new SqlCommand("insert into chatuser (mobileno,ServeiceType,exitflag) values('" + mobile_no + "','01',0)",sqlConnPHS_SMS);
int re=SqlInsertUserChat.ExecuteNonQuery();
MessageBox.Show(re.ToString());
}
}
DrReceiveBuff.Close();
timerReceive.Enabled=true;
}
运行时提示“以有打开的与此连接相关的 DataReader,必须首先将它关闭”
修改SqlCommand SqlInsertUserChat=new SqlCommand("insert into chatuser (mobileno,ServeiceType,exitflag) values('" + mobile_no +"','01',0)",sqlCon);
用两个连接就能正常运行。
难道我必须的用两个连接吗?如果多表操作岂不是需要更多连接。