By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 1,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

How best to close sqlclient.connection?

P: n/a
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.
Nov 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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.

"Paul W" <qq*@qqq.com> wrote in message
news:OV**************@TK2MSFTNGP11.phx.gbl...
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.

Nov 21 '05 #2

P: n/a
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

"Marina" <so*****@nospam.com> wrote in message
news:eJ*************@TK2MSFTNGP12.phx.gbl...
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.

"Paul W" <qq*@qqq.com> wrote in message
news:OV**************@TK2MSFTNGP11.phx.gbl...
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.


Nov 21 '05 #3

P: n/a
"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com>
schrieb:
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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #4

P: n/a
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.

"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com> wrote
in message news:eM**************@tk2msftngp13.phx.gbl...
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

"Marina" <so*****@nospam.com> wrote in message
news:eJ*************@TK2MSFTNGP12.phx.gbl...
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.

"Paul W" <qq*@qqq.com> wrote in message
news:OV**************@TK2MSFTNGP11.phx.gbl...
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.



Nov 21 '05 #5

P: n/a
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

"Paul W" <qq*@qqq.com> wrote in message
news:OV**************@TK2MSFTNGP11.phx.gbl...
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.

Nov 21 '05 #6

P: n/a
Here is a detailed and much nicer answer -
http://dotnetjunkies.com/WebLog/sahi.../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


"Sahil Malik" <co*****************@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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

"Paul W" <qq*@qqq.com> wrote in message
news:OV**************@TK2MSFTNGP11.phx.gbl...
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.


Nov 21 '05 #7

P: n/a
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
Nov 21 '05 #8

P: n/a
discussion in the ADONET newsgroup, not in a bar

:-)

Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.