Re: scope of command and garbage collection

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



"Smokey Grindle" <nospamhere@xxxxxxxxxxxx> 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/>

.



Relevant Pages