Is closing/disposing an SqlDataReader needed if you are already closing the sqlconnection?

Posted by Brian on Stack Overflow See other posts from Stack Overflow or by Brian
Published on 2010-04-16T20:41:22Z Indexed on 2010/04/16 20:43 UTC
Read the original article Hit count: 257

Filed under:
|
|
|
|

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 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 DataReader means leaving open connection resources around, but doesn't that only apply if you don't close the connection?

© Stack Overflow or respective owner

Related posts about sql

Related posts about c#