Does placing a SqlCommand.Connection in a using block close the connection in the same way as placing the SqlConnection in it’s own using block? (Is #1 = #2)
Example 1
using (var cmd = GetCommandWithConnectionSetInternally(connString))
{
using (cmd.Connection)
{
}
}
Example 2
using(var conn = new SqlConnection(connString))
{
using(var cmd = new SqlCommand(cmdText, conn))
{
}
}
So, does the connection in Example 1 get closed as it would in Example 2 upon exiting the “using” block?
I can’t see it working any other way since using is just a short form of
try {} finally {}with aDisposecall in the finally block. Since the resource object being used iscmd.Connection, it should get disposed.