473,473 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How best to close sqlclient.connection?

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
8 1891
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
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
"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
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
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
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
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
discussion in the ADONET newsgroup, not in a bar

:-)

Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve | last post by:
Hi, I am a relatively new user to vs.NET, and currently developing a ASP.NET web site using MS access, with the view to moving it to SQLServer. My problem is, how do I set up the connection to...
1
by: Bob | last post by:
Is this a good way to close DB Connection in ASP.NET? I've been trying to come up a way to close DB connection automatically (or sort of) in my ASP.NET application without having to write the...
2
by: Chris Langston | last post by:
I have a Web Server running IIS 5 or 6 on Windows 2K and Windows 2003 Server that is experiencing strange shutdown problems. We are using ASP.NET v1.1 and our application is written in VB.NET ...
4
by: mescano | last post by:
I am currently implementing a singleton pattern for accessing a database. Is it advisable to close the connection to the database at all -- thus leaving it open or should it be closed. If closed,...
8
by: Fredrik Melin | last post by:
I have a "Inventory" Object that contains the product and all its fields. The problem is that I am getting soooooo many functions under main Inventory class so it becames impossible to initalize...
5
by: csgraham74 | last post by:
Hi guys, Basically i have been developing in dotnet for a couple of years but ive had a few issues in regards to error handling. For example - I have a class that i call passing in a stored...
3
by: Cylix | last post by:
Does anyone can explain the details between Connection.close Connection.dispose Connection = nothing If I just need to close the connection and release the memory, do I need to write all...
51
by: bigHairy | last post by:
Hello. I have been teaching myself .NET over the last few months and have had some success. I would like to ask a question though... A number of examples I have followed have the following in...
5
by: darthghandi | last post by:
I've created a class to listen to all interfaces and do a BeginAccept(). Once it gets a connection, it passes the connected socket off and stores it in a List. Next, it continues to listen for...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.