When do DataReaders close?

  • Thread starter Thread starter Jon Davis
  • Start date Start date
J

Jon Davis

Don't Connections and DataReaders close automatically when destroyed? Do you
HAVE to close them manually, or is it just recommended?

Jon
 
You don't have to do anything. However, if you want to use the connection
associated with the reader then you'll need to close it. Depending on your
definition of Destroying, you don't know if or when it will be destroyed for
sure.

As far as connections...if you don't close them they don't go back into the
pool. If you wait around and hope, then you are wasting resources.
Considering that you can have the reader automatically close the connection
when it closes, it's really not a big deal.

Same goes for the reader. There's no upside to not closing stuff and a lot
of potential downside so trust me on this, go ahead and close both as soon
as you're done with them, it's a good habit and it will help your resource
allocation operate much more efficiently.

Even if you just throw 1 exception b/c of a connection being associate with
a connection when you try to reuse it, that's too many.

Cheers,

Bill

--
W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/
 
Please do not rely on automatic finalization to clean up connections and
other objects that hold system resources. ALWAYS explicitly Close() (or
Dispose()) connection objects. As for readers, you need to close/dispose
them if you want to use the connection for something else, otherwise when
you close the connection any open reader is also closed.

--
Pablo Castro
Program Manager - ADO.NET Team
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top