using (DbConnection conn = new DbConnection()) { // do stuff with database }
Will the using
block call conn.Close()
?
Answers. Yes. When the using block ends, the connection automatically closes (That is what IDisposable is for). So, do not close the connection explicitly.
If the application ends, the connection gets closed, along with everything else that was opened.
For the purpose of safe coding, you should always close database connections explicitly to make sure that the code was able to close itself gracefully and to prevent any other objects from reusing the same connection after you are done with it.
Yes, it will; the implementation of DbConnection.Dispose()
calls Close()
(and so do its derived implementations).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With