Solved

CLose a connection?

Posted on 2006-11-06
3
416 Views
Last Modified: 2008-03-06
You have an application that accesses a SQL Database utilizing the following code:

SqlConnection nwindConn = new SqlConnection("Data Source=localhost;
Integrated Security=SSPI; Initial Catalog=northwind");
nwindConn.Open();

What should you do to end the use of this Data Connection once you are finished with it?

Do nothing since the connection will be closed when Garbage Collection is performed on your object.
 
 
--------------------------------------------------------------------------------
 
  Close the connection directly by calling "nwindConn.Close();".
 
 
--------------------------------------------------------------------------------
 
  Close the connection directly by calling "nwindConn.Open = False;".
 
 
--------------------------------------------------------------------------------
 
  Do nothing because the connection is closed immediately after you pull data from the SQL Database.
 

0
Comment
Question by:mathieu_cupryk
  • 2
3 Comments
 
LVL 35

Expert Comment

by:Raynard7
ID: 17885756
Hi,

This depends if you have connection pooling enabled on your connection.

Garbage collection will not clean up a pooled connection -

alternativley I'd reccomend that you always close nwindConn.Close(); partially because your database may have a connection limit and if you are not utilising it then without calling that you are taking up an active connection.
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 250 total points
ID: 17885757
Close the connection directly by calling "nwindConn.Close();".
0
 
LVL 35

Expert Comment

by:Raynard7
ID: 17885783
How was my answer wrong? i said to call nwindConn.Close()
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question