Yes, because a using
statement is expanded by the compiler to be a try...finally
block. Your code is the same as this:
SqlConnection cn = null;
try {
cn = HostFacade.GetDbConnection();
cn.Open();
// .. rest of the code here
}
finally {
if (cn != null)
cn.Dispose();
}
The Dispose
call will close the connection.
The CLR guarantees that a finally block will execute.. except in very specific scenarios (such as a call to FailFast
IIRC).