A question about closing the SqlDataReader

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.
 
M

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())
{
// do something here
}
else
{
Response.Redirect("to_some_other_page.aspx");
}
}
 
J

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

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.
 
M

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

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.
 
J

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
 
M

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top