Search code examples
.netdisposeidatareader

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?


Solution

  • No; the connection will not be closed until you dispose the connection.

    However, if you pass CommandBehavior.CloseConnection, the connection will be closed.