SqlConnection not closing

  • Thread starter Thread starter Jimmy
  • Start date Start date
J

Jimmy

My brain must be going to sleep but can anyone tell me why after running
this code the database connection remains open?

I am testing some code and, after running these lines, when I try to drop
the database I cannot because this retains a connection. Shouldn't this
connection be dropped automatically? If not, how can I force the connection
to close?


using (SqlConnection testConnection = new SqlConnection())
{
testConnection.ConnectionString = string.Format("Data Source={0};Initial
Catalog={1};Integrated Security=SSPI;", server, database);
testConnection.Open();
}
 
Nope. When you use the Close method or the Using operator, the client
Connection object's state is changed to Closed, but the connection remains
open in the connection pool for 4-8 minutes unless it's reused. Either
disable connection pooling (connection string argument) or flush the pool to
force the connection to close (or stop the application).

--
__________________________________________________________________________
William R. Vaughn
President and Founder Beta V Corporation
Author, Mentor, Dad, Grandpa
Microsoft MVP
(425) 556-9205 (Pacific time)
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
____________________________________________________________________________________________
 
Back
Top