Home All Groups Group Topic Archive Search About

scope of command and garbage collection

Author
24 May 2006 12:36 PM
Smokey Grindle
Say I have the following piece of code

Dim dtPerson As New DataTable
Dim cmd As New SqlClient.SqlCommand("sp_test", database)
dtPerson.Load(cmd.ExecuteReader)
cmd.dispose()

which instanciates the command object, uses it then destroys it, now if i
rewrote it like this

Dim dtPerson As New DataTable
dtPerson.Load(New SqlClient.SqlCommand("test", database)

would that still act the same? because the commands scope is inside the load
method, shouldnt it be destroyed after the point of execution has passed the
load method? basicly creating a command that destroys itself after
execution? or is it better not to do it this way because that is not how it
works?

Author
24 May 2006 12:50 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Smokey Grindle" <nospamhere@dontspam.net> schrieb:
> Say I have the following piece of code
>
> Dim dtPerson As New DataTable
> Dim cmd As New SqlClient.SqlCommand("sp_test", database)
> dtPerson.Load(cmd.ExecuteReader)
> cmd.dispose()
>
> which instanciates the command object, uses it then destroys it, now if i
> rewrote it like this
>
> Dim dtPerson As New DataTable
> dtPerson.Load(New SqlClient.SqlCommand("test", database)
>
> would that still act the same? because the commands scope is inside the
> load method, shouldnt it be destroyed after the point of execution has
> passed the load method?

No, it won't.  It will be destoyed by the garbage collector later, but there
is no guarantee that it gets destroyed immediately.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
24 May 2006 1:04 PM
Jay B. Harlow [MVP - Outlook]
Smokey,
Addition to Herfried's comments.

In .NET 2.0 I would recommend:

| Dim dtPerson As New DataTable
    Using cmd As New SqlClient.SqlCommand("sp_test", database)
| dtPerson.Load(cmd.ExecuteReader)
    End Using

If .NET 1.x I would recommend:

| Dim dtPerson As New DataTable
| Dim cmd As New SqlClient.SqlCommand("sp_test", database)
Try
| dtPerson.Load(cmd.ExecuteReader)
Finally
| cmd.dispose()
End Try

This ensures that the SqlCommand is disposed of even if the DataTable.Load
method itself threw an exception.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"Smokey Grindle" <nospamhere@dontspam.net> wrote in message
news:eOBMA7yfGHA.5088@TK2MSFTNGP02.phx.gbl...
| Say I have the following piece of code
|
| Dim dtPerson As New DataTable
| Dim cmd As New SqlClient.SqlCommand("sp_test", database)
| dtPerson.Load(cmd.ExecuteReader)
| cmd.dispose()
|
| which instanciates the command object, uses it then destroys it, now if i
| rewrote it like this
|
| Dim dtPerson As New DataTable
| dtPerson.Load(New SqlClient.SqlCommand("test", database)
|
| would that still act the same? because the commands scope is inside the
load
| method, shouldnt it be destroyed after the point of execution has passed
the
| load method? basicly creating a command that destroys itself after
| execution? or is it better not to do it this way because that is not how
it
| works?
|
|
Author
24 May 2006 1:07 PM
Smokey Grindle
Yes, I was also looking at USING.

thanks both

Show quoteHide quote
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> wrote in
message news:eJEJTKzfGHA.2208@TK2MSFTNGP05.phx.gbl...
> Smokey,
> Addition to Herfried's comments.
>
> In .NET 2.0 I would recommend:
>
> | Dim dtPerson As New DataTable
>    Using cmd As New SqlClient.SqlCommand("sp_test", database)
> | dtPerson.Load(cmd.ExecuteReader)
>    End Using
>
> If .NET 1.x I would recommend:
>
> | Dim dtPerson As New DataTable
> | Dim cmd As New SqlClient.SqlCommand("sp_test", database)
> Try
> | dtPerson.Load(cmd.ExecuteReader)
> Finally
> | cmd.dispose()
> End Try
>
> This ensures that the SqlCommand is disposed of even if the DataTable.Load
> method itself threw an exception.
>
> --
> Hope this helps
> Jay B. Harlow [MVP - Outlook]
> .NET Application Architect, Enthusiast, & Evangelist
> T.S. Bradley - http://www.tsbradley.net
>
>
> "Smokey Grindle" <nospamhere@dontspam.net> wrote in message
> news:eOBMA7yfGHA.5088@TK2MSFTNGP02.phx.gbl...
> | Say I have the following piece of code
> |
> | Dim dtPerson As New DataTable
> | Dim cmd As New SqlClient.SqlCommand("sp_test", database)
> | dtPerson.Load(cmd.ExecuteReader)
> | cmd.dispose()
> |
> | which instanciates the command object, uses it then destroys it, now if
> i
> | rewrote it like this
> |
> | Dim dtPerson As New DataTable
> | dtPerson.Load(New SqlClient.SqlCommand("test", database)
> |
> | would that still act the same? because the commands scope is inside the
> load
> | method, shouldnt it be destroyed after the point of execution has passed
> the
> | load method? basicly creating a command that destroys itself after
> | execution? or is it better not to do it this way because that is not how
> it
> | works?
> |
> |
>
>
Author
24 May 2006 1:14 PM
Robin Mark Tucker
Hi Jay,

Can you explain a little more about the difference between the Using in .NET
2.0 and the .NET 1.1 version of the code?  I am new to .NET 2.0, but quite
experienced with .NET 1.1.  I had mostly used the Try/Catch method, but I'm
thinking .NET 2.0 wraps a hidden exception handler around the Using
statement for just such eventualities.  Is there an MSDN document about
this?

Thanks,


Robin

Show quoteHide quote
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> wrote in
message news:eJEJTKzfGHA.2208@TK2MSFTNGP05.phx.gbl...
> Smokey,
> Addition to Herfried's comments.
>
> In .NET 2.0 I would recommend:
>
> | Dim dtPerson As New DataTable
>    Using cmd As New SqlClient.SqlCommand("sp_test", database)
> | dtPerson.Load(cmd.ExecuteReader)
>    End Using
>
> If .NET 1.x I would recommend:
>
> | Dim dtPerson As New DataTable
> | Dim cmd As New SqlClient.SqlCommand("sp_test", database)
> Try
> | dtPerson.Load(cmd.ExecuteReader)
> Finally
> | cmd.dispose()
> End Try
>
> This ensures that the SqlCommand is disposed of even if the DataTable.Load
> method itself threw an exception.
>
> --
> Hope this helps
> Jay B. Harlow [MVP - Outlook]
> .NET Application Architect, Enthusiast, & Evangelist
> T.S. Bradley - http://www.tsbradley.net
>
>
> "Smokey Grindle" <nospamhere@dontspam.net> wrote in message
> news:eOBMA7yfGHA.5088@TK2MSFTNGP02.phx.gbl...
> | Say I have the following piece of code
> |
> | Dim dtPerson As New DataTable
> | Dim cmd As New SqlClient.SqlCommand("sp_test", database)
> | dtPerson.Load(cmd.ExecuteReader)
> | cmd.dispose()
> |
> | which instanciates the command object, uses it then destroys it, now if
> i
> | rewrote it like this
> |
> | Dim dtPerson As New DataTable
> | dtPerson.Load(New SqlClient.SqlCommand("test", database)
> |
> | would that still act the same? because the commands scope is inside the
> load
> | method, shouldnt it be destroyed after the point of execution has passed
> the
> | load method? basicly creating a command that destroys itself after
> | execution? or is it better not to do it this way because that is not how
> it
> | works?
> |
> |
>
>
Author
25 May 2006 12:13 AM
Jay B. Harlow [MVP - Outlook]
Robin,
The Using statement is effectively:

    Dim dtPerson As New DataTable

    Using cmd As New SqlClient.SqlCommand("sp_test", database)
    + Dim cmd As New SqlClient.SqlCommand("sp_test", database)
    + Try
        dtPerson.Load(cmd.ExecuteReader)
    End Using
    + Finally
    +    If cmd IsNot Nothing
    +        cmd.dispose()
    +    End If
    +End Try

Where the lines marked with + are what VB creates in the IL. You can use
ILDASM to see specifically what VB does.

The IsNot Nothing is there to allow for the case where cmd is set to nothing
within the Using block.

For details of the new statement see:

http://msdn2.microsoft.com/en-US/library/htd05whh.aspx

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"Robin Mark Tucker" <robintuckerhome@removehotmail.comremove> wrote in
message news:e51m7d$jl1$1$8302bc10@news.demon.co.uk...
|
| Hi Jay,
|
| Can you explain a little more about the difference between the Using in
..NET
| 2.0 and the .NET 1.1 version of the code?  I am new to .NET 2.0, but quite
| experienced with .NET 1.1.  I had mostly used the Try/Catch method, but
I'm
| thinking .NET 2.0 wraps a hidden exception handler around the Using
| statement for just such eventualities.  Is there an MSDN document about
| this?
|
| Thanks,
|
|
| Robin
|
| "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> wrote in
| message news:eJEJTKzfGHA.2208@TK2MSFTNGP05.phx.gbl...
| > Smokey,
| > Addition to Herfried's comments.
| >
| > In .NET 2.0 I would recommend:
| >
| > | Dim dtPerson As New DataTable
| >    Using cmd As New SqlClient.SqlCommand("sp_test", database)
| > | dtPerson.Load(cmd.ExecuteReader)
| >    End Using
| >
| > If .NET 1.x I would recommend:
| >
| > | Dim dtPerson As New DataTable
| > | Dim cmd As New SqlClient.SqlCommand("sp_test", database)
| > Try
| > | dtPerson.Load(cmd.ExecuteReader)
| > Finally
| > | cmd.dispose()
| > End Try
| >
| > This ensures that the SqlCommand is disposed of even if the
DataTable.Load
| > method itself threw an exception.
| >
| > --
| > Hope this helps
| > Jay B. Harlow [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > "Smokey Grindle" <nospamhere@dontspam.net> wrote in message
| > news:eOBMA7yfGHA.5088@TK2MSFTNGP02.phx.gbl...
| > | Say I have the following piece of code
| > |
| > | Dim dtPerson As New DataTable
| > | Dim cmd As New SqlClient.SqlCommand("sp_test", database)
| > | dtPerson.Load(cmd.ExecuteReader)
| > | cmd.dispose()
| > |
| > | which instanciates the command object, uses it then destroys it, now
if
| > i
| > | rewrote it like this
| > |
| > | Dim dtPerson As New DataTable
| > | dtPerson.Load(New SqlClient.SqlCommand("test", database)
| > |
| > | would that still act the same? because the commands scope is inside
the
| > load
| > | method, shouldnt it be destroyed after the point of execution has
passed
| > the
| > | load method? basicly creating a command that destroys itself after
| > | execution? or is it better not to do it this way because that is not
how
| > it
| > | works?
| > |
| > |
| >
| >
|
|
Author
24 May 2006 5:02 PM
Cor Ligthert [MVP]
Jay,

I would not use "using" without global exception handling or without forcing
the errors to the datarow.rowerror property.

I know that you use probably the first and I the last, however in my opinion
is it good to explain that.

Cor
Author
25 May 2006 12:09 AM
Jay B. Harlow [MVP - Outlook]
Cor,
Agree, using (no pun intended) a global exception handler is a good idea
along with appropriate "local" error handling if appropriate.


--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:%23GVZjO1fGHA.4940@TK2MSFTNGP05.phx.gbl...
| Jay,
|
| I would not use "using" without global exception handling or without
forcing
| the errors to the datarow.rowerror property.
|
| I know that you use probably the first and I the last, however in my
opinion
| is it good to explain that.
|
| Cor
|
|