|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
A question about closing the SqlDataReaderI 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()) Show quote > { > // do something here > } > else > { > Response.Redirect("to_some_other_page.aspx"); > } } -- Show quoteMiha 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. > 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. >> > > Hi Jing,
Sure, you are right. This was implied in my statements perhaps I didn't make it explicit. -- Show quoteMiha Markic [MVP C#] - RightHand .NET consulting & development www.rthand.com SLODUG - Slovene Developer Users Group www.codezone-si.info "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. >>> >> >> > > 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"); >> } >} 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, -- Show quoteMiha Markic [MVP C#] - RightHand .NET consulting & development www.rthand.com SLODUG - Slovene Developer Users Group www.codezone-si.info "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"); >>> } >>} > 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, |
|||||||||||||||||||||||