Closing a database connection

J

Jon

Hello, in the below, would the connection on dbCommand be
automatically closed once the code has left the using statement or is
it better that I close it?

using (DbCommand dbComm = db.GetStoredProcCommand("Es"))
{
some code
}

thank you,

Jon
 
G

gnewsgroup

Hello, in the below, would the connection on dbCommand be
automatically closed once the code has left the using statement or is
it better that I close it?

using (DbCommand dbComm = db.GetStoredProcCommand("Es"))
{
some code

}

thank you,

Jon
From the MSDN documentation, it looks like it will be automatically
disposed at the end of the using block. The principle seems to be
this: If the object is an instance of a class that implement the
IDisposable interface, and if it is declared in a using statement,
then it will be automatically disposed when it exits the using block.

In your case, DbCommand does implement the IDisposable interface,
among others, so the answer is yes.
 

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,774
Messages
2,569,598
Members
45,147
Latest member
CarenSchni
Top