c# exception - Will a using block close a database connection?

2 Answers

Yes, it will; the implementation of DbConnection.Dispose() calls Close() (and so do its derived implementations).

open not
using (DbConnection conn = new DbConnection())
    // do stuff with database

Will the using block call conn.Close()?

A using block will ensure the destruction of DbConnection object by calling the Dispose() method. The Dispose() method will in turn call the Close() method and has to wait for it to finish closing the connection to the database.


c# database using