|
|
|
start date: Thu, 16 Aug 2007 17:45:43 -0700,
posted on: microsoft.public.dotnet.framework.aspnet
back
| Thread Index |
|
1
WebBuilder451
|
|
2
Ladislav Mrnka
|
|
3
WebBuilder451
|
|
4
WebBuilder451
|
|
5
Peter Bromberg [C# MVP]
|
|
6
Ladislav Mrnka
|
|
7
WebBuilder451
|
question on sqldatareaders
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
--
thanks (as always)
some day i''m gona pay this forum back for all the help i''m getting
kes
Date:Thu, 16 Aug 2007 17:45:43 -0700
Author:
|
RE: question on sqldatareaders
Hi,
you should always close your SqlDataReader as soon as possible. Best
practice is to use try/finally block.
Best regards,
Ladislav
"WebBuilder451" wrote:
> 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
>
> --
> thanks (as always)
> some day i''m gona pay this forum back for all the help i''m getting
> kes
Date:Thu, 16 Aug 2007 17:48:25 -0700
Author:
|
RE: question on sqldatareaders
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)
> > If rdr.HasRows Then
> > rdr.Read()
......
......
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
"Peter Bromberg [C# MVP]" wrote:
> You need to explicitly Close your DataReaders because they hold open
> connections. Also, if you intend only to Call the Close method of the reader,
> it needs to have been created with CommandBehavior.CloseConnection in order
> for that to close the connection as well.
> -- Peter
> Recursion: see Recursion
> site: http://www.eggheadcafe.com
> unBlog: http://petesbloggerama.blogspot.com
> BlogMetaFinder: http://www.blogmetafinder.com
>
>
>
> "WebBuilder451" wrote:
>
> > 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
> >
> > --
> > thanks (as always)
> > some day i''m gona pay this forum back for all the help i''m getting
> > kes
Date:Thu, 16 Aug 2007 18:01:38 -0700
Author:
|
RE: question on sqldatareaders
thank you
--
thanks (as always)
some day i''m gona pay this forum back for all the help i''m getting
kes
"Ladislav Mrnka" wrote:
> Hi,
> you should always close your SqlDataReader as soon as possible. Best
> practice is to use try/finally block.
>
> Best regards,
> Ladislav
>
> "WebBuilder451" wrote:
>
> > 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
> >
> > --
> > thanks (as always)
> > some day i''m gona pay this forum back for all the help i''m getting
> > kes
Date:Thu, 16 Aug 2007 18:07:37 -0700
Author:
|
RE: question on sqldatareaders
You need to explicitly Close your DataReaders because they hold open
connections. Also, if you intend only to Call the Close method of the reader,
it needs to have been created with CommandBehavior.CloseConnection in order
for that to close the connection as well.
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com
"WebBuilder451" wrote:
> 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
>
> --
> thanks (as always)
> some day i''m gona pay this forum back for all the help i''m getting
> kes
Date:Thu, 16 Aug 2007 18:12:25 -0700
Author:
|
RE: question on sqldatareaders
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
"WebBuilder451" wrote:
> 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)
> > > If rdr.HasRows Then
> > > rdr.Read()
> ......
> ......
> 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
>
>
> "Peter Bromberg [C# MVP]" wrote:
>
> > You need to explicitly Close your DataReaders because they hold open
> > connections. Also, if you intend only to Call the Close method of the reader,
> > it needs to have been created with CommandBehavior.CloseConnection in order
> > for that to close the connection as well.
> > -- Peter
> > Recursion: see Recursion
> > site: http://www.eggheadcafe.com
> > unBlog: http://petesbloggerama.blogspot.com
> > BlogMetaFinder: http://www.blogmetafinder.com
> >
> >
> >
> > "WebBuilder451" wrote:
> >
> > > 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
> > >
> > > --
> > > thanks (as always)
> > > some day i''m gona pay this forum back for all the help i''m getting
> > > kes
Date:Fri, 17 Aug 2007 00:02:03 -0700
Author:
|
RE: question on sqldatareaders
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
"Ladislav Mrnka" wrote:
> 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
>
> "WebBuilder451" wrote:
>
> > 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)
> > > > If rdr.HasRows Then
> > > > rdr.Read()
> > ......
> > ......
> > 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
> >
> >
> > "Peter Bromberg [C# MVP]" wrote:
> >
> > > You need to explicitly Close your DataReaders because they hold open
> > > connections. Also, if you intend only to Call the Close method of the reader,
> > > it needs to have been created with CommandBehavior.CloseConnection in order
> > > for that to close the connection as well.
> > > -- Peter
> > > Recursion: see Recursion
> > > site: http://www.eggheadcafe.com
> > > unBlog: http://petesbloggerama.blogspot.com
> > > BlogMetaFinder: http://www.blogmetafinder.com
> > >
> > >
> > >
> > > "WebBuilder451" wrote:
> > >
> > > > 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
> > > >
> > > > --
> > > > thanks (as always)
> > > > some day i''m gona pay this forum back for all the help i''m getting
> > > > kes
Date:Fri, 17 Aug 2007 06:22:10 -0700
Author:
|
|
|