Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does IDataReader.Dispose() close the connection?

The typical query execution pattern I inherited is like so:

using (IDataReader r = query.ExecuteReader())
{
  while (r.Read())
  {
    // etc.
  }
}

Is query.Connection left open after the using block is exited?

like image 286
svidgen Avatar asked Oct 25 '25 14:10

svidgen


1 Answers

ExecuteReader(CommandBehavior.CloseConnection)

This will close the connection when the datareader has it's close() method called (which occurs when the dispose() method is called through usage of the using block.

Ideally, you would use a using block with your SqlConnection object as well (or call dispose() manually inside a finally block), not just to close the connection, but also to release the resources.

like image 69
Brad M Avatar answered Oct 28 '25 03:10

Brad M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!