scope of command and garbage collection

  • Thread starter Thread starter Smokey Grindle
  • Start date Start date
S

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?
 
Smokey Grindle said:
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.
 
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


| 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?
|
|
 
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,

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
 
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


| 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
|
|
 
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


message |
| 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
|
| message | > 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
| >
| >
| > | > | 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?
| > |
| > |
| >
| >
|
|
 
Back
Top