I noticed This question, but my question is a bit more specific.
Is there any advantage to using
using (SqlConnection conn = new SqlConnection(conStr))
{
using (SqlCommand command = new SqlCommand())
{
// dostuff
}
}
instead of
using (SqlConnection conn = new SqlConnection(conStr))
{
SqlCommand command = new SqlCommand();
// dostuff
}
Obviously it does matter if you plan to run more than one command with the same connection, since closing an SqlDataReader
is more efficient than closing and reopening a connection (calling conn.Close();conn.Open();
will also free up the connection).
I see many people insist that failure to close the SqlDataReader
means leaving open connection resources around, but doesn't that only apply if you don't close the connection?