ASP.NET - using(sqlcommand cmd=new sqlcommand)

Asked By madhu radha on 27-Jul-10 08:25 AM
hii all,
  
  can u tel me the difference between
sqlcommand cmd=new sqlcommand();
and
using(sqlcommand cmd=new sqlcommand());

same as for sqldataadapter also

shital patil replied to madhu radha on 27-Jul-10 08:36 AM
hi,

using(SqlCommand cmd = new SqlCommand())

{

    ...

}

is equivalent to writing,

SqlCommand cmd = new SqlCommand();

try

{

    ...

}

 

finally

{

    cmd.Dispose();

}

 

Santosh Prajapati replied to madhu radha on 27-Jul-10 08:55 AM
Object.Dispose()
Mash B replied to madhu radha on 27-Jul-10 09:05 AM
Actually Using statement defines a scope, outside of which an object or objects will be disposed.
So its avoid overhead of disposing the object manually in finally block or end of working with object.
tim mccarthy replied to Mash B on 27-Jul-10 09:38 AM
Great post! informative and useful sharing.