In my opinion, there are two rules:
- Classes that implement IDisposable must be wrapped in a
using block. - You should not rely on the implementation of the IDisposable class to ignore rule 1.
That is, even if you know that deleting the connection object took care of disposing of the command object associated with it, you should not rely on this behavior.
By the way, you can use the nest blocks cleaner:
using (SqlConnection conn = new SqlConnection(conStr)) using (SqlCommand command = new SqlCommand()) {
and i would use
SqlCommand command = conn.CreateCommand();
instead of creating a new SqlCommand, and then link it to the connection.
source share