Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As usual the answer is: it depends. Different classes implement <code>IDisposable</code> in different ways, and it's up to you to do the necessary research.</p> <p>As far as <code>SqlClient</code> goes, the recommended practice is to do the following:</p> <pre><code>using (SqlConnection conn = /* Create new instance using your favorite method */) { conn.Open(); using (SqlCommand command = /* Create new instance using your favorite method */) { // Do work } conn.Close(); // Optional } </code></pre> <p>You <strong>should</strong> be calling <code>Dispose</code> (or <code>Close</code>*) on the connection! Do <strong>not</strong> wait for the garbage collector to clean up your connection, this will tie up connections in the pool until the next GC cycle (at least). If you call <code>Dispose</code>, it is not necessary to call <code>Close</code>, and since the <code>using</code> construct makes it so easy to handle <code>Dispose</code> correctly, there is really no reason to call <code>Close</code>.</p> <p>Connections are automatically pooled, and calling <code>Dispose</code>/<code>Close</code> on the connection does not physically close the connection (under normal circumstances). Do not attempt to implement your own pooling. <code>SqlClient</code> performs cleanup on the connection when it's retrieved from the pool (like restoring the database context and connection options).</p> <p>*if you are calling <code>Close</code>, make sure to do it in an exception-safe way (i.e. in a catch or finally block).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload