For your second code, Dispose
won't be called on SqlConnection
instance when flow leaves using
block unless SqlCommand.Dispose()
do that internally (and no, it doesn't).
According to specification (8.13), using (ResourceType resource = expression) statement
is transformed into:
{
ResourceType resource = expression;
try {
statement;
}
finally {
if(resource != null)
((IDisposable)resource).Dispose();
}
}
In your code, resource
is SqlCommand
, and that's the one Dispose
is called on.