2

Possible Duplicate:
Trying to understand the ‘using’ statement better

I've really read all other posts, but no one really answers my question.

This is my function that returns a table

        public DataTable ReturnTable()
        {
            DataTable dt = new DataTable();   
            using (SqlConnection con = new SqlConnection(mainConnectionString))
            {
                con.Open();                             
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandType = CommandType.Text;    
                    SQL = " SELECT * from table";                        
                    cmd.CommandText = SQL;                                            
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        da.Fill(dt);
                    }
                }
            }           
            return dt;
        }

what's the advantage of the previous against the following (the one that I always used prior to discover 'using'):

public DataTable ReturnTable()
            {
                DataTable dt = new DataTable();   
                SqlConnection con = new SqlConnection(mainConnectionString);
                con.Open();                             
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;    
                SQL = " SELECT * from table";                        
                cmd.CommandText = SQL;                                            
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                con.Close();
                return dt;
            }

With the second, aren't con, cmd and da properly disposed? Is there anything wrong with the second?

Thanks!

Community
  • 1
  • 1
Mattia Durli
  • 757
  • 7
  • 19
  • The connection object is the most important one to close or dispose since it holds physical database resources. It will not be closed until the GC finalizes the object in the second case if there is an exception. Disposing the command and the data adapter is a really good idea but not as critical. – Dmitry S. Dec 15 '12 at 03:58

4 Answers4

4

Is there anything wrong with the second?

As long as you don't get an exception in your code, this will work the same way for con, since Close() and Dispose() are effectively the same in this case. It will not dispose of da or cmd immediately, and will wait until they are garbage collected to free their resources.

That advantage of using is that your resource is still disposed even in the case of exceptions or early exit from the method (you adding a return in the middle of your method).

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
0

The advantage is that the using pattern calls the Dispose() method on the IDisposable interface, guaranteeing that any cleanup logic you may have missed gets properly executed, even if an exception is thrown.

In practice, objects implementing IDisposable are holding onto unmanaged resources which get cleaned up when Dispose() is called. So calling Close() may not be sufficient.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
0

No, they're not the same. That's the whole point of using.

What happens if your code throws an exception? Those objects won't be disposed until the garbage collector happens to get around to it.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
0

"using" in in this case tries to implement the RAII pattern, which is especially useful when dealing with limited resources such as database connections.

Zar Shardan
  • 5,675
  • 2
  • 39
  • 37