Home All Groups Group Topic Archive Search About

A question about closing the SqlDataReader

Author
30 Mar 2005 6:54 PM
M
Hi all,

I have code like:
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
    // do something here
}
else
{
    reader.Close();        // this is the line I'm not sure about
    Response.Redirect("to_some_other_page.aspx");
}
reader.Close();


My question is:
Is reader.Close() in the else part really needed? If reader.Read() returns
nothing, then is the reader actually open or not?

The code above does not crash, but maybe the Close() is redundant...

Thanks.

Author
30 Mar 2005 7:37 PM
Miha Markic [MVP C#]
Yes, it is necessary to close it.
More slick way would be (note that you don't need explicit Close call - it
is handled through implicit Dispose):

using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
Show quote
> {
>    // do something here
> }
> else
> {
>    Response.Redirect("to_some_other_page.aspx");
> }
}

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info


Show quote
"M" <someb***@somewhere.com> wrote in message
news:uwDounVNFHA.3192@TK2MSFTNGP10.phx.gbl...
> Hi all,
>
> I have code like:
> SqlDataReader reader = cmd.ExecuteReader();
> if (reader.Read())
> {
>    // do something here
> }
> else
> {
>    reader.Close();        // this is the line I'm not sure about
>    Response.Redirect("to_some_other_page.aspx");
> }
> reader.Close();
>
>
> My question is:
> Is reader.Close() in the else part really needed? If reader.Read() returns
> nothing, then is the reader actually open or not?
>
> The code above does not crash, but maybe the Close() is redundant...
>
> Thanks.
>
Author
31 Mar 2005 1:50 PM
JiangZemin
Hi, i would suggest that the "using" is more than slick but very necessary,
either that or putting the datareader in a try-catch-finally block to handle
possible error during reading (basically same thing as using "using").
Otherwise the reader doesnt get closed at all.

HTH,
Premier JiangZemin

Show quote
"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
news:OqzTl$VNFHA.2748@TK2MSFTNGP09.phx.gbl...
> Yes, it is necessary to close it.
> More slick way would be (note that you don't need explicit Close call - it
> is handled through implicit Dispose):
>
> using (SqlDataReader reader = cmd.ExecuteReader())
> {
> if (reader.Read())
>> {
>>    // do something here
>> }
>> else
>> {
>>    Response.Redirect("to_some_other_page.aspx");
>> }
> }
>
> --
> Miha Markic [MVP C#] - RightHand .NET consulting & development
> www.rthand.com
> SLODUG - Slovene Developer Users Group www.codezone-si.info
>
>
> "M" <someb***@somewhere.com> wrote in message
> news:uwDounVNFHA.3192@TK2MSFTNGP10.phx.gbl...
>> Hi all,
>>
>> I have code like:
>> SqlDataReader reader = cmd.ExecuteReader();
>> if (reader.Read())
>> {
>>    // do something here
>> }
>> else
>> {
>>    reader.Close();        // this is the line I'm not sure about
>>    Response.Redirect("to_some_other_page.aspx");
>> }
>> reader.Close();
>>
>>
>> My question is:
>> Is reader.Close() in the else part really needed? If reader.Read()
>> returns nothing, then is the reader actually open or not?
>>
>> The code above does not crash, but maybe the Close() is redundant...
>>
>> Thanks.
>>
>
>
Author
31 Mar 2005 2:17 PM
Miha Markic [MVP C#]
Hi Jing,

Sure, you are right.
This was implied in my statements perhaps I didn't make it explicit.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

Show quote
"JiangZemin" <fourpill***@example.com> wrote in message
news:u4uNJifNFHA.244@tk2msftngp13.phx.gbl...
> Hi, i would suggest that the "using" is more than slick but very
> necessary,
> either that or putting the datareader in a try-catch-finally block to
> handle possible error during reading (basically same thing as using
> "using"). Otherwise the reader doesnt get closed at all.
>
> HTH,
> Premier JiangZemin
>
> "Miha Markic [MVP C#]" <miha at rthand com> wrote in message
> news:OqzTl$VNFHA.2748@TK2MSFTNGP09.phx.gbl...
>> Yes, it is necessary to close it.
>> More slick way would be (note that you don't need explicit Close call -
>> it is handled through implicit Dispose):
>>
>> using (SqlDataReader reader = cmd.ExecuteReader())
>> {
>> if (reader.Read())
>>> {
>>>    // do something here
>>> }
>>> else
>>> {
>>>    Response.Redirect("to_some_other_page.aspx");
>>> }
>> }
>>
>> --
>> Miha Markic [MVP C#] - RightHand .NET consulting & development
>> www.rthand.com
>> SLODUG - Slovene Developer Users Group www.codezone-si.info
>>
>>
>> "M" <someb***@somewhere.com> wrote in message
>> news:uwDounVNFHA.3192@TK2MSFTNGP10.phx.gbl...
>>> Hi all,
>>>
>>> I have code like:
>>> SqlDataReader reader = cmd.ExecuteReader();
>>> if (reader.Read())
>>> {
>>>    // do something here
>>> }
>>> else
>>> {
>>>    reader.Close();        // this is the line I'm not sure about
>>>    Response.Redirect("to_some_other_page.aspx");
>>> }
>>> reader.Close();
>>>
>>>
>>> My question is:
>>> Is reader.Close() in the else part really needed? If reader.Read()
>>> returns nothing, then is the reader actually open or not?
>>>
>>> The code above does not crash, but maybe the Close() is redundant...
>>>
>>> Thanks.
>>>
>>
>>
>
>
Author
31 Mar 2005 4:35 PM
J L
I am a newbie and using VB.Net. I had understood that Using was
equivalent to Imports. I know read in the online help that it also can
be used to define scope, as you are indicating here. Is there an
equivalent in VB.Net to this construct?

TIA
John

On Wed, 30 Mar 2005 21:37:19 +0200, "Miha Markic [MVP C#]" <miha at
rthand com> wrote:

Show quote
>Yes, it is necessary to close it.
>More slick way would be (note that you don't need explicit Close call - it
>is handled through implicit Dispose):
>
>using (SqlDataReader reader = cmd.ExecuteReader())
>{
>if (reader.Read())
>> {
>>    // do something here
>> }
>> else
>> {
>>    Response.Redirect("to_some_other_page.aspx");
>> }
>}
Author
31 Mar 2005 4:47 PM
Miha Markic [MVP C#]
Hi J L,

using has two meanings in C#:
- one is equivalent to imports
- the other is a substitute for try/finally block (this was what I've meant)

using (something = new someclass())
{
}
is equivalent to

somethin = new someclass();
try
{
    ...
}
finally
{
    something.Dispose();
}

There is no similar construct in VB.NET so you are stuck with try/finally
construct.

HTH,
--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info


Show quote
"J L" <j***@marymonte.com> wrote in message
news:jk9o419sjufprucl861er4ehai7he2ofth@4ax.com...
>I am a newbie and using VB.Net. I had understood that Using was
> equivalent to Imports. I know read in the online help that it also can
> be used to define scope, as you are indicating here. Is there an
> equivalent in VB.Net to this construct?
>
> TIA
> John
>
> On Wed, 30 Mar 2005 21:37:19 +0200, "Miha Markic [MVP C#]" <miha at
> rthand com> wrote:
>
>>Yes, it is necessary to close it.
>>More slick way would be (note that you don't need explicit Close call - it
>>is handled through implicit Dispose):
>>
>>using (SqlDataReader reader = cmd.ExecuteReader())
>>{
>>if (reader.Read())
>>> {
>>>    // do something here
>>> }
>>> else
>>> {
>>>    Response.Redirect("to_some_other_page.aspx");
>>> }
>>}
>
Author
31 Mar 2005 5:24 PM
J L
Yes that does help immensely. Thanks,
John

On Thu, 31 Mar 2005 18:47:37 +0200, "Miha Markic [MVP C#]" <miha at
rthand com> wrote:

Show quote
>Hi J L,
>
>using has two meanings in C#:
>- one is equivalent to imports
>- the other is a substitute for try/finally block (this was what I've meant)
>
>using (something = new someclass())
>{
>}
>is equivalent to
>
>somethin = new someclass();
>try
>{
>    ...
>}
>finally
>{
>    something.Dispose();
>}
>
>There is no similar construct in VB.NET so you are stuck with try/finally
>construct.
>
>HTH,

AddThis Social Bookmark Button