If a SqlConnection
throws an exception during execution inside a using
statement, do I need to manually close the connection in a finally
? or will the scope of the using
statement call the Dispose
method (on the SqlConnection
) for me ... which therefore executes the .Close();
method for me (automatically)?
for example:
using (var sqlConnection = new SqlConnection(_connectionString)
{
sqlConnection.Open();
throw new Exception("boom!");
}
vs
using (var sqlConnection = new SqlConnection(_connectionString)
{
try
{
sqlConnection.Open();
throw new Exception("boom!");
}
finally
{
sqlConection.Close();
}
}
Also, does wrapping this in a TransactionScope
+ an exception is throw, affect how I should .Close()
or the using
scope auto-does this for me.