我正在使用一个计时器,它每隔1毫秒导致一次与数据库的连接,有时在关闭前一个连接之前,它会连接至数据库,因此这会引起问题。
有没有办法解决这个问题?
` protected void timerTest_tick(object sen, EventArgs e)
{
string sql = "SELECT * FROM [TableActions] WHERE ID =" + Session["LobbyID"];
Connection cn = new Connection();
OleDbDataReader reader = cn.GetReader(sql);
if (reader.Read())
{
Session["GNACode"] = reader.GetString(2);
}
cn.closecon();
}`
您应该将Connection cn = new Connection();
移至类范围
并移动cn.closecon();
以形成关闭事件。
public class YourForm{
private Connection cn = new Connection();
protected void timerTest_tick(object sen, EventArgs e)
{
string sql = "SELECT * FROM [TableActions] WHERE ID =" + Session["LobbyID"];
OleDbDataReader reader = cn.GetReader(sql);
if (reader.Read())
{
Session["GNACode"] = reader.GetString(2);
}
}
private void CloseEvent(){
cn.closecon();
}
}