Does DataReader.Close() method close the connection object when using Microsoft Application Blocks for Data Access?

Jacob Sebastian picture Jacob Sebastian · Jun 29, 2011 · Viewed 14.2k times · Source

I am reviewing an application that uses Microsoft Application Blocks for Data Access to interact with the database. The application calls a function and pass a query into it. The function creates a DataReader object using Data Access application blocks (for the given query) and returns the DataReader to the caller.

The application closes DataReader object when it is done. The question I have is, whether closing the DataReader object automatically closes the underlying connection object or not. It is not clear to me whether the Data Access Application Block opens the command object with the "CommandBehavior.CloseConnection" flag.

Questions:

  1. When the Close() method is called on a DataReader object, does it also close the underlying connection object (the DataReader is created using Microsoft Application Blocks - Database.ExecuteReader() method
  2. If not, what is the recommended method to ensure that the connection is freed after we are done with the data reader?

Thanks in advance

Answer

http://msdn.microsoft.com/en-us/library/system.data.commandbehavior.aspx

CloseConnection: When the command is executed, the associated Connection object is closed when the associated DataReader object is closed.