66

If I wrap a SQLConnection in a Using, should I close it or does the end using handle it?

using cn as new system.data.sqlclient.sqlconnection()
    cn.open
    '{do a bunch of other stuff with commands and datareaders here}
    cn.close 'Do I need this?
end using 

6 Answers 6

96

Exiting a using block calls .Dispose() on the object in question (cn in your example) which for a SqlConnection will close the connection and any open resources.

3
  • 3
    What will happen if an exception is thrown within the using block? will the connection still get disposed?
    – Youssef
    May 21, 2010 at 19:46
  • 10
    Joel Coehoorn's YES. is ambiguous. the full answer is : "You don't need to close because the end using handle it" Jun 29, 2011 at 0:06
  • 1
    AFAIK, it does call .Dispose() on it, but it will be closed only if connection pooling is not active. With connection pooling the connection usually returns to the pool, or gets closed if some "clean-up" criteria are met, so you can say nothing in general.. as a matter of fact from your point of view it is "closed", since it becomes available to the pool for the next one needing one..
    – Luke
    May 26, 2015 at 13:54
31

More precisely calling Dispose or Close will mark the underlying physical connection as "Not in use" - but doesn't really close it. A "Not in use" connection that isn't yet physically closed is thus available for pooling. Therefore - calling Dispose would return a connection to the connection pool.

2
  • 1
    Sorry.. you say calling Dispose or Close "doesn't really close it", then you say "calling Dispose would return a connection to the pool". Can you clarify? a) Does Dispose both close it and return it to the pool? b) Does Close also return it to the pool after closing it? Or will it only go back to the pool after a Dispose? May 23, 2012 at 15:36
  • 2
    Why does the wrong answer has been checked ? it is clearly known that close/dispose doesn't actually close the connection back bring it back to the pool.
    – Royi Namir
    May 25, 2014 at 13:26
13

According to MSDN you don't need the close statement.

"The following example creates a SqlConnection, opens it, displays some of its properties. The connection is automatically closed at the end of the using block." -- http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.close.aspx

4

While the SQL's Dispose method does close the connection (eventually according to darin) you should leave the call to Close in there. The reason is that you would be relying on the underlying implementation of Dispose to call close. Also seeing an Open without a Close is like seeing a New without a Delete for those of us that have programmed in unmanaged languages. It's a code smell for me.

3
  • I agree with the code smell. I like to program explicitly, not implicitly.
    – Astra
    Dec 17, 2008 at 22:54
  • 24
    I disagree. It's just noise. Learn the new paradigm with using blocks.
    – TrueWill
    Aug 18, 2010 at 20:17
  • I hate the saying "code smell" I do not know why people think it is ok to use lol. In my opinion you are being anal retentive, the using statement closing bracket expresses the Disposing of the resource the using statement uses. You could be argue the explicit Close call adds 3 lines to the code file, possibly causing the using statement block to be large enough that you can no longer see the first line of the using block in a code editor and this can be more inconvenient to the developers delicate sensibilities than an explicit Close statement because there is an Open statement Jun 24, 2017 at 22:34
2

using is just a shorthand to try/finally. this is equivilent code to what you posted

Try
    SqlConnection cn as new system.data.sqlclient.sqlconnection()
    cn.open
    '{do a bunch of other stuff with commands and datareaders here}
    cn.close 'Do I need this?
Finally
    cn.Dispose()
End Try

Dispose is supposed to take care of all resource cleanup, in the case of connections it will close it.

2

"A Using block behaves like a Try...Finally construction in which the Try block uses the resources and the Finally block disposes of them. Because of this, the Using block guarantees disposal of the resources, no matter how you exit the block. This is true even in the case of an unhandled exception, except for a StackOverflowException."
https://msdn.microsoft.com/en-us/library/htd05whh.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.