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
 
J

Juan T. Llibre

Correct.

I would add that the Command object also needs
to be disposed of, not only the Connection object.

So, not only is connection.close() needed,
but command.close() is needed, too.
 

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

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top