Home All Groups Group Topic Archive Search About

Closing of data reader..............

Author
26 Jun 2006 10:29 PM
Lowry Smith
MyReader.ExecuteReader(CommandBehavior.CloseConnection)



I am using the above statement for the reader object. Closing of connection
object is taken care in the above statement. Do I have to close the 'Reader'
exclusively?

..Net 1.1.

Thank you,

Smith

Author
27 Jun 2006 1:00 AM
Carl Prothman
"Lowry Smith" <LowrySmit***@hotmail.com> wrote
> MyReader.ExecuteReader(CommandBehavior.CloseConnection)
> I am using the above statement for the reader object. Closing of connection
> object is taken care in the above statement.
>
> Do I have to close the 'Reader' exclusively?
>

Yes, if the SqlDataReader is created with CommandBehavior set to CloseConnection, closing the SqlDataReader closes the connection automatically.
http://msdn2.microsoft.com/en-us/library/y6wy5a0f.aspx

--

Thanks,
Carl Prothman
Microsoft ASP.NET MVP
http://www.CarlProthman.NET
Author
27 Jun 2006 6:30 AM
Carl Prothman
"Carl Prothman" <c***@prothman.online.org> wrote
> "Lowry Smith" <LowrySmit***@hotmail.com> wrote
>> MyReader.ExecuteReader(CommandBehavior.CloseConnection)
>> I am using the above statement for the reader object. Closing of connection
>> object is taken care in the above statement.
>> Do I have to close the 'Reader' exclusively?
>
> Yes, if the SqlDataReader is created with CommandBehavior set to CloseConnection, closing the SqlDataReader closes the connection automatically.
> http://msdn2.microsoft.com/en-us/library/y6wy5a0f.aspx
>

Note there is one exception to this rule when using CommandBehavior.CloseConnection.  When you bind the SqlDataReader to an ASP.NET control (e.g. DataGrid), the connection is closed after the ExecuteReader method has returned all of the records.

--

Thanks,
Carl Prothman
Microsoft ASP.NET MVP
http://www.CarlProthman.NET
Author
27 Jun 2006 7:38 AM
Miha Markic [MVP C#]
Show quote
"Carl Prothman" <c***@prothman.online.org> wrote in message
news:uCAr5MbmGHA.4512@TK2MSFTNGP04.phx.gbl...
> "Carl Prothman" <c***@prothman.online.org> wrote
>> "Lowry Smith" <LowrySmit***@hotmail.com> wrote
>>> MyReader.ExecuteReader(CommandBehavior.CloseConnection)
>>> I am using the above statement for the reader object. Closing of
>>> connection
>>> object is taken care in the above statement.
>>> Do I have to close the 'Reader' exclusively?
>>
>> Yes, if the SqlDataReader is created with CommandBehavior set to
>> CloseConnection, closing the SqlDataReader closes the connection
>> automatically.
>> http://msdn2.microsoft.com/en-us/library/y6wy5a0f.aspx
>>
>
> Note there is one exception to this rule when using
> CommandBehavior.CloseConnection.  When you bind the SqlDataReader to an
> ASP.NET control (e.g. DataGrid), the connection is closed after the
> ExecuteReader method has returned all of the records.

Isn't that always the case? I mean you can close a SqlDataReader only when
it has fetched all records. If you close it before, it will still fetch all
records and then close.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
Author
27 Jun 2006 6:32 PM
Lowry Smith
I think I was not clear about my question.
The question is, whether I have to write the below statement or not?
MyReader.Close()
Since I am using 'CommandBehavior.CloseConnection', once the data are
fetched completely, the connection is closed. And alll the associated
resources are released (in this case Data Reader)??.
Now, Is it necessary to call MyReader.Close?
Thank you for your answers,
Smith




Show quote
"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
news:O%23x$$ybmGHA.2452@TK2MSFTNGP04.phx.gbl...
>
> "Carl Prothman" <c***@prothman.online.org> wrote in message
> news:uCAr5MbmGHA.4512@TK2MSFTNGP04.phx.gbl...
> > "Carl Prothman" <c***@prothman.online.org> wrote
> >> "Lowry Smith" <LowrySmit***@hotmail.com> wrote
> >>> MyReader.ExecuteReader(CommandBehavior.CloseConnection)
> >>> I am using the above statement for the reader object. Closing of
> >>> connection
> >>> object is taken care in the above statement.
> >>> Do I have to close the 'Reader' exclusively?
> >>
> >> Yes, if the SqlDataReader is created with CommandBehavior set to
> >> CloseConnection, closing the SqlDataReader closes the connection
> >> automatically.
> >> http://msdn2.microsoft.com/en-us/library/y6wy5a0f.aspx
> >>
> >
> > Note there is one exception to this rule when using
> > CommandBehavior.CloseConnection.  When you bind the SqlDataReader to an
> > ASP.NET control (e.g. DataGrid), the connection is closed after the
> > ExecuteReader method has returned all of the records.
>
> Isn't that always the case? I mean you can close a SqlDataReader only when
> it has fetched all records. If you close it before, it will still fetch
all
> records and then close.
>
> --
> Miha Markic [MVP C#, INETA Country Leader for Slovenia]
> RightHand .NET consulting & development www.rthand.com
> Blog: http://cs.rthand.com/blogs/blog_with_righthand/
>
>
Author
27 Jun 2006 8:31 PM
Miha Markic [MVP C#]
Yes, why not?
CloseConnection means to close the connection when the reader is closed -
and unless you dispose or/and close datareader it isn't closed at that time.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Show quote
"Lowry Smith" <LowrySmit***@hotmail.com> wrote in message
news:%233PTOghmGHA.4512@TK2MSFTNGP04.phx.gbl...
>I think I was not clear about my question.
> The question is, whether I have to write the below statement or not?
> MyReader.Close()
> Since I am using 'CommandBehavior.CloseConnection', once the data are
> fetched completely, the connection is closed. And alll the associated
> resources are released (in this case Data Reader)??.
> Now, Is it necessary to call MyReader.Close?
> Thank you for your answers,
> Smith
>
>
>
>
> "Miha Markic [MVP C#]" <miha at rthand com> wrote in message
> news:O%23x$$ybmGHA.2452@TK2MSFTNGP04.phx.gbl...
>>
>> "Carl Prothman" <c***@prothman.online.org> wrote in message
>> news:uCAr5MbmGHA.4512@TK2MSFTNGP04.phx.gbl...
>> > "Carl Prothman" <c***@prothman.online.org> wrote
>> >> "Lowry Smith" <LowrySmit***@hotmail.com> wrote
>> >>> MyReader.ExecuteReader(CommandBehavior.CloseConnection)
>> >>> I am using the above statement for the reader object. Closing of
>> >>> connection
>> >>> object is taken care in the above statement.
>> >>> Do I have to close the 'Reader' exclusively?
>> >>
>> >> Yes, if the SqlDataReader is created with CommandBehavior set to
>> >> CloseConnection, closing the SqlDataReader closes the connection
>> >> automatically.
>> >> http://msdn2.microsoft.com/en-us/library/y6wy5a0f.aspx
>> >>
>> >
>> > Note there is one exception to this rule when using
>> > CommandBehavior.CloseConnection.  When you bind the SqlDataReader to an
>> > ASP.NET control (e.g. DataGrid), the connection is closed after the
>> > ExecuteReader method has returned all of the records.
>>
>> Isn't that always the case? I mean you can close a SqlDataReader only
>> when
>> it has fetched all records. If you close it before, it will still fetch
> all
>> records and then close.
>>
>> --
>> Miha Markic [MVP C#, INETA Country Leader for Slovenia]
>> RightHand .NET consulting & development www.rthand.com
>> Blog: http://cs.rthand.com/blogs/blog_with_righthand/
>>
>>
>
>

AddThis Social Bookmark Button