Advertise with us

SqlCommand with using statement

Posted by  john rob
 1339  View(s)
Rate this:

I saw that in most samples SqlCommand was used like this

using (SqlConnection con = new SqlConnection(CNN_STRING))
    using (SqlCommand cmd = new SqlCommand("Select ID,Name From Person", con))
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds =new DataSet();
        return ds;

I know why we are using "using" statement. But SqlCommand doesn't inlcude Close() method, so should we really use it within using statement

  1. Re: SqlCommand with using statement

    SqlCommand does implement IDisposable which a using statement will call .Dispose() on before the using block is finished. it's a good idea to call .Dispose() on an instance you are finished with i.e. it will clear up a database connection perhaps.