문제

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?

도움이 되었습니까?

해결책 2

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

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top