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

Total Post:108

Posted on    August-18-2014 3:17 AM

 C# C# 

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

Pravesh Singh

Total Post:412

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