Does End Using close an open SQL Connection
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
Solution 1:
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.
Solution 2:
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.
Solution 3:
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