SqlConnection not closing

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();
}
 
W

William Vaughn \(MVP\)

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)
____________________________________________________________________________________________
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top