c# - Do I need to close SQL Server connection with the using keyword? -
I am looking for conflicting results for this question. See this C # code of running SQL queries:
using (SqlConnection cn = new SqlConnection (strConnectString)) {cn.Open (); (Using SqlCommand cmd = new SqlCommand (strSQL, cn)) {cmd.ExecuteNonQuery (); } // do I need to call? Cn.Close (); } Do I have to call that last cn.close () ? I'm asking that in a heavy traffic web application I run out of connections in a pool.
Using as the keyword : Using (for SqlConnection cn = New SqlConnection (strConnectString)) {// Stuff} :
SqlConnection cn = null; Try {cn = new SqlConnection (strConnectString); // Stuff} finally {if (cn! = Null) cn.dejas (); } cn.Dispose () after immediately will be out of the scope of cn By using Code>> , which in turn closes the connection (because SqlConnection.Dispose () does). UPDATE This should not be confused with garbage collection. GC is non-determinant in NAT, which is why the integration and presentation of IDisposable IDisposable allows expensive resources to be released in a constructive manner on time.
Comments
Post a Comment