新连接被拒绝,因为已达到最大连接数

时间:2017-09-29 14:51:50

标签: c# sql-server database-connection

我在C#中编写了一个代码,用于从本地数据库获取数据,操作然后将其与第三方同步。

我在应用程序启动时打开连接,然后在我的所有调用结束时关闭它(因为我需要在循环中打开一个连接)。我正在使用数据加载器和数据表,并在使用后正确处理它们。

using (DataTable dataTableLocation = new DataTable()) //this is inside a loop
{
    using (SqlCommand cmd = new System.Data.SqlClient.SqlCommand(queryLocation, conn))
    {
       cmd.CommandType = System.Data.CommandType.StoredProcedure;
       cmd.Parameters.Add(new SqlParameter("@id", id));

       // Use the above SqlCommand object to create a SqlDataReader object.
       using (SqlDataReader queryCommandReader3 = cmd.ExecuteReader())
       {
            // Use the DataTable.Load(SqlDataReader) function to put the results of the query into a DataTable.
            dataTableLocation.Load(queryCommandReader3);
       }

    }
    //Data manipulation and then sync
}

我已将Max Pool Size设置为800. 4小时后我的控制台应用程序崩溃了,当我查看windows事件时,它说

  

新连接被拒绝,因为已达到会话ID 63上的最大连接数。关闭此会话上的现有连接,然后重试

在进行调用时,我不断监视数据库以查看活动连接,它显示7-8

select * from sys.dm_os_performance_counters where counter_name ='User Connections'

我是否必须处理,关闭连接,然后在每个循环中重新创建它?

数据库连接代码

string connectionString = Properties.Settings.Default.connectionString;
SqlConnection  conn = new SqlConnection(connectionString);

0 个答案:

没有答案