Home > DeveloperSection > Forums > SqlCommand with using statement
john rob

Total Post:108

Points:756
Posted on    August-18-2014 3:17 AM

 C# C# 
Ratings:


 1 Reply(s)
 578  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();

        da.Fill(ds);          

        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



Pravesh Singh

Total Post:411

Points:2881
Posted on    August-18-2014 3:38 AM

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.

Don't want to miss updates? Please click the below button!

Follow MindStick