Re: closing DataReader in another layer

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: DS (nojunkmail.signups_at_rogers.com)
Date: 11/26/04


Date: Fri, 26 Nov 2004 02:28:05 -0500


"Michael Giagnocavo [MVP]" <mggUNSPAM@atrevido.net> wrote in message
news:O9RNe$30EHA.3416@TK2MSFTNGP09.phx.gbl...
> Try this:
>
> theReader = theConnection.ExecuteReader(CommandBehavior.CloseConnection);
> return theReader;
>
> CommandBehaviour.CloseConnection makes the reader so that when the reader
> is closed, the underlying connection gets closed too. This allows you to
> call Close() on the reader somewhere else, and not leak a connection.
>
> However, you might consider DataSets for a different reason: They actually
> can go across physical layers, not just logical ones.
>
> --
> Michael Giagnocavo
> MVP
> www.atrevido.net

Thanks for the reply Michael,
I'd like to not even have to call the .Close on the reader in the business
tier as it is easy to forget to do, especially down the road. With a dataset
its not a big deal since I can close the connection in the DAL before the
dataset even gets returned and never have to worry about it in the buiness
layer. With a dataset if I forget to dispose it, at least that gets taken
care of by the garbage collection, whilst I believe a connection close does
not happen correctly on garbage collection of the DataReader.

Cheers,
Dan



Relevant Pages

  • Re: garbage collection
    ... it will get a reader back.. ... you can not reuse the connection on which it opened. ... > garbage collection issue, its a logical error. ... > on database connection is, ...
    (microsoft.public.dotnet.framework.aspnet)
  • Three Phases To Email Sensitivity
    ... the associationphase, the connection phase, and the ... They should have the right words thatthe reader can transform back ... When writing your response, you willwant to make ... If the topic is about apples, you donot want to add an orange ...
    (comp.lang.vhdl)
  • Re: garbage collection
    ... You can not return reader after the connection is closed. ... But to return a dataReader you need to keep the connection open. ... Also, If your application>> doesnt allow database connection pooling, you need to dispose your> database ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: CommandBehavior.CloseConnection question
    ... > When you close the reader, the connection is closed if you use ... Dim dbReader as SqlDataReader ... Dim ConnectionString as String ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Multi-threaded apps and data readers
    ... RightHand .NET consulting & development www.rthand.com ... Blog: http://cs.rthand.com/blogs/blog_with_righthand/ ... So if I kept the connection open all ... commands reader, how would I prevent the readers from clashing or do ...
    (microsoft.public.dotnet.framework.adonet)