question on sqldatareaders

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

given i have a class with a data reader function that returns a reader with a:
function fn_getUserInfo() as sqlDataReader
....
....
....
Return sqlcmd1.ExecuteReader(Data.CommandBehavior.CloseConnection)
end

and the following code for using this reader:
Dim lf As loginfunctions = New loginfunctions
Dim rdr As SqlDataReader = lf.fn_getUserInfo(Page.User.Identity.Name)
If rdr.HasRows Then
rdr.Read()
....
....
....
end

Do i need to close this reader at the end of execution or will it go away
after it goes out of scope?

Thanks
 
Hi,
you should always close your SqlDataReader as soon as possible. Best
practice is to use try/finally block.

Best regards,
Ladislav
 
all my readers are created this way in a data layer class with the
CommandBehavior.CloseConnection. It was this that i was told would kill the
reader after it want out of scope in the calling routine.
so in the:
Dim rdr As SqlDataReader = lf.fn_getUserInfo(Page.User.Identity.Name)......
......
i need to add a rdr.close() ?
--
thanks (as always)
some day i''m gona pay this forum back for all the help i''m getting
kes
 
Hi,
lifetime of all objects is handled internally by .net framework where all
objects are destroyed (and freed from memory) by garbage collector. If you
close data reader you release or resources you needed to run it - in your
case you also close SqlConnection and release all its resources but you
cannot be 100 percent sure where these objects are collected and destroyed by
garbage collector - it will be after these objects go out of scope but you
don't know when. If you do not explicitly close your reader (and connection)
all resources remain allocated until garbage collector destroys SqlDataReader
and SqlConnection instances. This is reason why you should close these
instances as soon as possible to free your resources and make them availible
to your application.

I hope I have explained it correctly.

Regards,
Ladislav
 
yes you have and it is very appreciated.
Thank You
--
thanks (as always)
some day i''m gona pay this forum back for all the help i''m getting
kes
 
Back
Top