close db connections

H

Howard

using (SqlConnection connection = new SqlConnection(constr))
{
using (SqlCommand command = new SqlCommand(com, connection))
{
//read from db
}
}


Is this sufficient to close db connections or do I need to add
connection.close()..

I know the object will be disposed after this statement but will it close
open db connections?

What is the best practice for running a sql query in asp.net

Thanks,
Howard
 
C

Chris Fulstow

Hi Howard,

Dispose() will close your connection so there's not need to Close() as
well. Unlike Close(), Dispose() will also clear the connection's
ConnectionString, so be careful if you try and Open() it again.

HTH,

Chris
 

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