I have a C# Windows service that repeatedly calls a SQL Server stored procedure through the code below. When running the stored procedure manually, it returns the appropriate data set in under one second.
When the service runs, I can see it executing the stored procedure multiple times in sys.dm_exec_requests
and sys.sysprocess
, and then finishing properly.
However, viewing it in sys.dm_exec_sessions
, the connections are still staying open and seem to max out at 50 sessions, even though the Max Pool Size
set in the connection string is set at 250.
In researching this, it seems that I shouldn't need to close or dispose of the connection when using a USING
statement (there's a good explanation here). But this doesn't seem to be working for me. I've also tried doing it without the USING
but with a TRY...FINALLY
including the CLOSE()
. However this didn't work either.
In all cases, I can see the stored procedures execute and finish but the connections seem to stay open. Am I missing something? Is there any way I can make sure to close the connections? I understand what the error in the subject is telling me, but my attempts to resolve it aren't working.
DataTable dt = new DataTable();
try
{
string connStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
using (SqlConnection conn = new SqlConnection(connStr))
{
SqlCommand sqlComm = new SqlCommand(SQL, conn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = sqlComm;
da.SelectCommand.CommandTimeout = 720;
da.Fill(dt);
}
}
catch (Exception ex)
{
SendErrorEmail("GetData function", ex.ToString());
}
return dt;
Here's the connection string I'm using...
<add key="ConnectionString" value="Data Source=*.*.*.*;Database=*****;Persist Security Info=True;User ID=*****;Password=*****;Max Pool Size=250;Application Name=SQLService;" />