473,725 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How best to close sqlclient.conne ction?

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

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 1901
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******** ******@TK2MSFTN GP11.phx.gbl...
Hi - in an asp.net application, how should I close out a
sqlclient.conne ction?

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******** *****@TK2MSFTNG P12.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******** ******@TK2MSFTN GP11.phx.gbl...
Hi - in an asp.net application, how should I close out a
sqlclient.conne ction?

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.Disp ose()
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******** ******@tk2msftn gp13.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******** *****@TK2MSFTNG P12.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******** ******@TK2MSFTN GP11.phx.gbl...
Hi - in an asp.net application, how should I close out a
sqlclient.conne ction?

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******** ******@TK2MSFTN GP11.phx.gbl...
Hi - in an asp.net application, how should I close out a
sqlclient.conne ction?

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.co m> wrote in message
news:%2******** ********@TK2MSF TNGP15.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******** ******@TK2MSFTN GP11.phx.gbl...
Hi - in an asp.net application, how should I close out a
sqlclient.conne ction?

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.dispo se. However the connection.disp ose 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
1148
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 be utilise connection pooling, and to be easily maintained and modified. I was thinking of setting up a DSN, which is how I am currently connecting in ASP but I'm not sure how to do it in .NET. So I created a class which opens a connection and...
1
1464
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 close() method everywhere. Every web page in my application needs to call the database, and all DB access and SQL Command code are wrapped in a separate class (called DBAccess below). I don't want to open and close the connection within every...
2
4587
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 Here's the scenario: 1. .NET Windows Client on a remote machine makes a web service call to update tables on a Web Server running SQL Server 2000. 2. The Update is updating about 1000 - 3000 records doing simple update statements like "Update...
4
3978
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, when should I it close it -- after the execution of the each command to the database? If leaving open, what impacts does it have. Imagining that it is one connection to the database. Thanks, mescano
8
1777
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 a new programmer into this code. For sample, I have Add_AllowAccess, Add_DisallowAccess, Remove_AllowAccess, Remove_DisallowAccess, Get_AllowAccess, Get_DisallowAccess (this is only a sample) I would really like
5
1735
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 procedure and connection string as a path. My method returns a dataset. In my SP i have an output parameter which tells me whether the SP select is successful or not. If i get a error code passed back then i throw an exception this then returns...
3
4612
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 three statement? Thank you.
51
3650
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 their finally statement Try ......
5
12798
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 more incoming connections and does the BeginAccpet() again. It does an infinite loop this way. My question is: What is the best way to stop this? I thought about putting a boolean in there, but then what if it's still waiting for an incoming...
0
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.