A question about closing the SqlDataReader

  • Thread starter Thread starter M
  • Start date Start date
M

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.
 
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");
}
}
 
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 said:
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 said:
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.
 
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

JiangZemin said:
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 said:
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 said:
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.
 
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
 
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,
 
Back
Top