Disposing Objects

  • Thread starter Thread starter Ken Getz
  • Start date Start date
K

Ken Getz

Suppose I have a Function/Procedure like this:

Sub DoSomething()
Dim db As New SqlClient.SqlConnection(ConnectionString)
db.Open()
Dim cmd As New SqlClient.SqlCommand("Delete from emp where EmpID=1",
db)
cmd.ExecuteNonQuery()
db.Close
cmd.Dispose()
db.Dispose()
End Sub


In this procedure I have disposed the Connection object and Command Object
once I am done using it.
Do I have to do the dispose, or the garbase collector will take care of
this.

Is it a good practice to dispose objects once we are done using it.

Ken
 
Suppose I have a Function/Procedure like this:

Sub DoSomething()
Dim db As New SqlClient.SqlConnection(ConnectionString)
db.Open()
Dim cmd As New SqlClient.SqlCommand("Delete from emp where EmpID=1",
db)
cmd.ExecuteNonQuery()
db.Close
cmd.Dispose()
db.Dispose()
End Sub


In this procedure I have disposed the Connection object and Command Object
once I am done using it.

Well, you have if no exception is thrown... you should really use a
Try/Finally block to close the connection and the command even if an
exception is thrown.
Do I have to do the dispose, or the garbase collector will take care of
this.

You should call Dispose or Close - they're likely to do the same thing
in most situations. Personally I use Dispose for consistency across
different classes.
Is it a good practice to dispose objects once we are done using it.

Yes, if they implement IDisposable and *if* you "own" the object.
 
Nothing like responding to a question from yourself (no, honestly, I'm
a DIFFERENT Ken Getz, really. Just wanted to respond to clear things
up. There are apparently two of us.)

You should dispose of things when you're done with them, as a rule, but
it's more complicated than that, 'cause there are many issues involved.
If you're using C# or VB 2005, you can take advantage of the Using
statement, like this:

Using db As New SqlClient.SqlConnection(ConnectionString)
db.Open
Using cmd As New SqlClient.SqlCommand(CommandString)
cmd.ExecuteNonQuery
End Using
End Using

If you're using VB.NET 2003, you'll need to make sure you enclose your
Dispose method calls in a Finally block, so they always get executed,
even if an exception occurs, like this:

Dim cmd As SqlCommand
Dim db As SqlConnection

Try
' Put your code here that instantiates the objects.
Finally
If Not cmd Is Nothing Then
cmd.Dispose
End If
If Not db Is Nothing THen
db.Dispose
End If
End try

-- Ken (the other one)
 
Nothing like responding to a question from yourself (no, honestly, I'm
a DIFFERENT Ken Getz, really. Just wanted to respond to clear things
up. There are apparently two of us.)

You should dispose of things when you're done with them, as a rule, but
it's more complicated than that, 'cause there are many issues involved.
If you're using C# or VB 2005, you can take advantage of the Using
statement, like this:

Using db As New SqlClient.SqlConnection(ConnectionString)
db.Open
Using cmd As New SqlClient.SqlCommand(CommandString)
cmd.ExecuteNonQuery
End Using
End Using

If you're using VB.NET 2003, you'll need to make sure you enclose your
Dispose method calls in a Finally block, so they always get executed,
even if an exception occurs, like this:

Dim cmd As SqlCommand
Dim db As SqlConnection

Try
' Put your code here that instantiates the objects.
Finally
If Not cmd Is Nothing Then
cmd.Dispose
End If
If Not db Is Nothing THen
db.Dispose
End If
End try

-- Ken (the other one)
 
Nothing like responding to a question from yourself (no, honestly, I'm
a DIFFERENT Ken Getz, really. Just wanted to respond to clear things
up. There are apparently two of us.)

You should dispose of things when you're done with them, as a rule, but
it's more complicated than that, 'cause there are many issues involved.
If you're using C# or VB 2005, you can take advantage of the Using
statement, like this:

Using db As New SqlClient.SqlConnection(ConnectionString)
db.Open
Using cmd As New SqlClient.SqlCommand(CommandString)
cmd.ExecuteNonQuery
End Using
End Using

If you're using VB.NET 2003, you'll need to make sure you enclose your
Dispose method calls in a Finally block, so they always get executed,
even if an exception occurs, like this:

Dim cmd As SqlCommand
Dim db As SqlConnection

Try
' Put your code here that instantiates the objects.
Finally
If Not cmd Is Nothing Then
cmd.Dispose
End If
If Not db Is Nothing THen
db.Dispose
End If
End try

-- Ken (the other one)
 
Back
Top