Usually I’d do this:
using (SqlCommand cmd = new SqlCommand("XXXX", cnn))
{
using (SqlDataReader dr = cmd.ExecuteReader())
{
//xxxxxx
}
}
Or even this:
using (SqlCommand cmd = new SqlCommand("XXXX", cnn))
using (SqlDataReader dr = cmd.ExecuteReader())
{
//xxxxxx
}
But what about this:
using (SqlDataReader dr = new SqlCommand("XXXX", cnn).ExecuteReader())
{
//xxxxxx
}
Does that call Dispose() for the SqlCommand, even though I haven’t assigned it to a variable?
No. It will call
Dispose()on theSqlDataReader(dr), since that is the object returned by the expression.