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!
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.
You must explicitly call the Close method when you are through using the SqlDataReader to use the associated SqlConnection for any other purpose.
The best way is to use the Using -statement which ensures that unmanaged resources are disposed(even on error). This also closes the reader.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With