使用的DataReader我获得以下错误:超时过期错误:在一个循环
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll
Additional information: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
这是当我试图读取每秒使用定时器从数据库中的值。
以下是代码。我知道这是因为SQL连接没有关闭。但是我能做些什么来解决它?
public void showcount2(int ID)
{
notifyIcon1.Visible = true;
string count;
string connString = ConfigurationManager.ConnectionStrings["Dbconn"].ToString();
SqlConnection connection = new SqlConnection(connString); // defining sql connection
connection.Open(); // opening connection
SqlCommand cmd1 = connection.CreateCommand();
cmd1.CommandText = "select count from dbo.tblcount where UserID = " + ID;
DataSet datasetFBK1 = new DataSet();
SqlDataAdapter dataadapterFBK1 = new SqlDataAdapter(cmd1);
dataadapterFBK1.Fill(datasetFBK1);
SqlDataReader dr1 = cmd1.ExecuteReader();
if (dr1.Read())
{
string countcheck;
countcheck = notifyIcon2.Text;
count = dr1[0].ToString();
notifyIcon2.Text = "NCT COUNT FOR COLLEAGUE IS: " + count;
if (countcheck == notifyIcon2.Text)
{
return;
}
else
{
notifyIcon2.BalloonTipText = "NCT COUNT FOR COLLEAGUE IS: " + count;
notifyIcon2.ShowBalloonTip(50000);
}
//pick a colored icon based on the count of the NCT cases
if (Convert.ToInt32(count) <= 3)
{
notifyIcon2.Icon = new Icon("C:\\BackupFromPC\\greenicon.ico");
}
else if (Convert.ToInt32(count) > 3 && Convert.ToInt32(count) <= 5)
{
notifyIcon2.Icon = new Icon("C:\\BackupFromPC\\yellowicon.ico");
}
else if (Convert.ToInt32(count) > 5)
{
notifyIcon2.Icon = new Icon("C:\\BackupFromPC\\redicon.ico");
}
//------------------------------
connection.Close();
}
}
谢谢你的回复。这工作得很好。感谢您的意见。 –