question on sqldatareaders

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
 
G

Guest

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

Best regards,
Ladislav
 
G

Guest

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
 
G

Guest

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
 
G

Guest

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
 

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

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top