Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close reader before closing connection

Should you always close a DataReader before closing the connection, or is it okay to just close the connection? By just closing the connection does that effectively close the reader?

(This is for c#.net 3.5)

Thanks!

like image 843
Dan H Avatar asked Mar 25 '10 21:03

Dan H


People also ask

Does closing DataReader close connection?

Using the Close() method of the data provider's Connection object adds or returns the connection to the connection pool. Remember, however, that closing a connection automatically closes all DataReader objects associated with the connection.

Do I need to close DataReader?

You must explicitly call the Close method when you are through using the SqlDataReader to use the associated SqlConnection for any other purpose.

How do I close a reader in VB net?

The best way is to use the Using -statement which ensures that unmanaged resources are disposed(even on error). This also closes the reader.


1 Answers

Aside from making your intent clear, there is this from the Microsoft documentation (Retrieving Data Using a DataReader (ADO.NET)):

You should always call the Close method when you have finished using the DataReader object.

like image 92
Thomas Avatar answered Oct 12 '22 23:10

Thomas