CLR 2.0, SQl objects and dispose

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

Hi,
I know this has been discussed before and I have read the earlier posts but
I am still unclear.
I have code that was written in 1.1

Dim cmdConn As SqlClient.SqlCommand

Dim cmdSource As SqlClient.SqlDataReader

Dim other stuff

try

Instantiate the objects and do the business

Catch

logging error etc

Finally

cmdSource.Close()

cmdConn.Dispose()

End Try

Now in CLR 2.0 I am warned that the finally block is using unassigned
variables. (Which it is)

I am tempted to ditch the finally clause and leave it to the Garbage
collector.

What is the recommended code structure in this situation.

Thanks

Bob
 
Hi Maarten,
No, this doesn't work. The warning goes from unitialised to Error
undeclared variables.
regards
Bob
 
Further to my last post, I am going to put the disposes inside the try
block.
If there is an error then I'll leave it to the GC to clean up after the
Catches clause has done its thing.
Unless...
Some one has a better idea.
Thanks
Bob
 
try
dim cmdConn as SqlClient.SqlCommand = nothing
Dim cmdSource as SqlClient.SqlDataReader = nothing
etc
etc
 
Back
Top