How best to close sqlclient.connection?

  • Thread starter Thread starter Paul W
  • Start date Start date
P

Paul W

Hi - in an asp.net application, how should I close out a
sqlclient.connection?

What combination and order of

Conn.close
conn.dispose
conn=nothing

Should I use? Specifically, is the .dispose required? Thanks,

Paul.
 
conn.Close() will suffice.

Setting to object to nothing just creates extra code that has no effect. The
garbage collector will collect your objects when they get out of scope.
 
There have been several discussions about using .dispose whenever you finish
with an object that has a dispose. Any reason not to use it here?

Chris
 
Chris said:
There have been several discussions about using .dispose whenever you
finish with an object that has a dispose. Any reason not to use it here?

I suggest to use 'Dispose' instead of 'Close' if you want to dispose the
connection. Calling 'Dispose' on the connection will implicitly call
'Close' if the connection is not yet closed:

\\\
Dim Connection As SqlConnection
Try
Connection = New SqlConnection()
Connection.Open()

' Use the connection here.
Catch

' Place error handling code here.
Finally
If Not Connection Is Nothing Then
Connection.Dispose()
End If
End Try
///
 
Yes, there have been many. Despite the fact that supposedly all Dispose does
is call Close, some still see reasons to call Dispose.

To me, calling Close is more readable as to what it is doing.

But like I said, Close should suffice as far as closing the connection and
returning it back to the pool. That doesn't mean you can't call Dispose
instead if you feel that is better.
 
Paul --- Just call dispose. Dispose calls Close internally.

The reason I say "Dispose" and not "Just call Close" is because in future
Microsoft might put more clean up logic in Dispose, than just close. You'd
miss out on that otherwise :)

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
 
Here is a detailed and much nicer answer -
http://dotnetjunkies.com/WebLog/sahilmalik/archive/2004/12/31/40036.aspx

This question comes up fairly often, and I feel dispose is a much better
choice for fairly compelling reasons described above.

If it's any consolation, either "close" or "dispose" if you choose any one
of them, you won't completely wreck your app. You would already be doing the
most awesome and important thing with DB Connections - i.e. to close them
soon as you can. So the question here is "How should I close, by using
dispose or by using close". My View is "Dispose" and I have my reasons
described in the above post.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
 
Marina,

I still keep it for this on dispose, I had a little discussion with Angel, I
wrongly understood him by the way, he was talking about the command and I
misreaded it because the thread was about connection.

After that he admitted that the dispose in the command was not overloaded
and just doing the componend.dispose. However the connection.dispose is. And
you know what he write forever about more than 100 connection.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top