0

Are ALL the sqlCommand, sqlTransaction and sqlDataAdapter objects automatically disposed on exit from the "using" block (in which sqlConnection disposed) or all these objects to be disposed should be in separated nested "using" blocks, otherwise these objects will not be disposed when sqlConnection object disposed? What are the best practices?

public static void ExecuteDataset(string connectionString, string storedProcedure, SqlParameter[] sqlParameters = null)
{
    SqlTransaction sqlTransaction = null;

    using (SqlConnection sqlConnection = new SqlConnection(connectionString))
    {
        try
        {
            sqlConnection.Open();

            SqlCommand sqlCommand = new SqlCommand(commandText, connection);
            command.Parameters.Add("@ID", SqlDbType.Int);

            sqlCommand.Connection = sqlConnection;
            SqlTransaction = sqlConnection.BeginTransaction();
            sqlCommand.Transaction = sqlTransaction;
            .....................................................
            .....................................................    

            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();

                ................................
                ................................


            sqlTransaction.Commit();
        }
        catch (Exception)
        {
            sqlTransaction.Rollback();
        }
    }        
}
18
  • 1
    General rule: if it's IDisposable, then Dispose of it when you're done Commented Nov 15, 2017 at 8:01
  • @ic. Ok, but my question is whether disposing of sqlConnection automatically disposes all the rest objects (slqTransaction, etc) or they should be in nested "using" blocks? Commented Nov 15, 2017 at 8:11
  • 1
    Which is why I wrote as a comment instead of an answer :). In general though I don't worry about the inner workings of things, especially since they're prone to change with each version. If something I create is disposable it means I should dispose it. Commented Nov 15, 2017 at 8:16
  • 2
    @Ilan That is an implementation detail that you should not need to know. Dispose anything your create that is Disposable, preferably with a using block. Anything else is a shortcut that you can do... but it's simply sloppy programming and will fall apart if you change something (like for example your database provider). Commented Nov 15, 2017 at 11:17
  • 2
    Maybe. Maybe not. That's the point. You don't know. I don't know. Even if we looked up the implementation to know, the database vendor could change it with the next patch. What we do know is that each of the classes implement IDisposable so you should put them in a using block. That way, they will be handled properly, whatever the database vendor considers "properly". Commented Nov 15, 2017 at 11:26

1 Answer 1

3

If the object implements IDisposable then use using. The underlying implementation details of SqlConnection or SqlCommand or any other object should not matter.

For your scenario the best-practice would be to

using(var conn = new SqlConnection(...)) {
  ...
  using(var cmd = conn.CreateCommand(...)) {
    ...
  }
  ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.