Solved

CLose a connection?

Posted on 2006-11-06
3
414 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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