2017-03-23 19 views
1

使用的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(); 
      } 

     } 

回答

1

I know it's because the SQL connection is not closed. But what could I possibly do to solve it?

好,做关闭。这样做最简单的方法将是封闭的连接使用的using块:

using(SqlConnection connection = new SqlConnection(connString)) 
{ 
    // the rest of your code goes here 
} 

using语句转换为try/finally块。在finally区块(当您return或其他离开示波器时将始终执行)connection将自动关闭。

+0

谢谢你的回复。这工作得很好。感谢您的意见。 –

1

您应该使用:-) 您退出范围时所使用的所有内容都将被丢弃。 包括关闭SqlConnections

像这样:

using (SqlConnection connection = new SqlConnection(connString)) 
    { 
     using (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"); 
       } 
       //------------------------------     
      } 
     } 
    } 
+0

谢谢你的回复。你的解决方案看起来不错但是我已经从@Rene得到了这个建议。然而,一旦我完成了这个项目以理解这个工作流程,我会试一试。感谢您的回复。再次感谢! –