Solved

CLose a connection?

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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

773 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