[thelist] Weird SQL problem with INTERSECT and UNION

Jason Handby jasonh at pavilion.co.uk
Wed Apr 2 07:28:27 CST 2003


> thus, the example given in the page jason linked to is rather
> inadequate --

Sorry -- it was the best I could hurriedly google!


J


<tip author="Jason Handby" type="C#">
It's important to ensure that your database connection objects get closed as
soon as possible so that they release any resources they're using. This is
particularly an issue in the .NET framework because of the way objects are
managed, which can give rise to a significant delay between your code
finishing with an object and that object being destroyed.

An easy way to do this in C# is to use "using", e.g.

  using (SqlConnection conn = new SqlConnection("my connection string"))
  {
    SqlCommand cmd = new SqlCommand("SELECT ID, Name, Description FROM
MyTable", conn);
    SqlDataReader dr = cmd.ExecuteReader();
    // do some stuff here
    dr.Close();
    conn.Close();
  }

Even if your code falls over before reaching  conn.Close()  the connection
object will be closed automatically when execution leaves the using() block.
This will work with any class that implements the IDisposable interface.
</tip>



More information about the thelist mailing list