同时处理两次连接到数据库

问题描述 投票:0回答:1

我正在使用一个计时器,它每隔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();
    }`
c# oledbconnection
1个回答
0
投票

您应该将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();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.