Link to home
Start Free TrialLog in
Avatar of mathieu_cupryk
mathieu_cuprykFlag for Canada

asked on

CLose a connection?

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.
 

Avatar of Raynard7
Raynard7

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.
ASKER CERTIFIED SOLUTION
Avatar of ozymandias
ozymandias
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How was my answer wrong? i said to call nwindConn.Close()