Disposing CommandObjects and Connections

  • Thread starter Thread starter Charles A. Lackman
  • Start date Start date
C

Charles A. Lackman

Hello,

I was just wondering, when reusing a Command Object and Connection, what is
the best way of releasing their resources? i.e.

Dim AConnection As New OledbConnection
Dim ACommand As New OledbCommand

Does the following make a new object in place of the old or does it make 2
objects for each?

AConnection = New OledbConnection
ACommand = New OledbCommand

It is better to do the following?

ACnnection.Dispose
ACommand.Dispose

AConnection = New OledbConnection
ACommand = New OledbCommand

Thanks,
Chuck
 
The main thing to remember - very very important is to use your Connections
in a using( ) block in C# and/or make sure you use a Try/Catch/Finally in
VB.NET where you close the connection in the finally.This is an area that
can cause you a lot of trouble, and if it's not in the finally block (the
close) you may be skipping over it and draining precious resources.

Calling .Dispose isn't a bad idea and I do it personally when I'm in VB.NET.
And yes, the New keyword will create a new instance of an object.

HTH,

Billl

--

W.G. Ryan, eMVP

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/community/newsgroups
 
Back
Top