mathieu_cupryk
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
How was my answer wrong? i said to call nwindConn.Close()
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.