Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

disposing the objects 1

Status
Not open for further replies.

compu66

Programmer
Dec 19, 2007
71
US
Hi all,

If I write cmd.connection.dispose()
In this only connection object gets destroyed ,or even command object also gets destroyed.

Thanks for any help in advance.
 
cmd.connection.dispose() will dispose of the connection. you still need to dispose of the command. use the using keyword to dispose of objects. this is a shorthand for try/finally blocks. example.
this
Code:
using (IDbConnection cnn = new SqlConnection())
{
   IDbCommand cmd = new SqlCommand("select * from foo", cnn);
   cmd.ExecuteReader();
}
equals this
Code:
IDbConnection cnn = new SqlConnection()
IDbCommand cmd = new SqlCommand("select * from foo", cnn);
try
{
   cmd.ExecuteReader();
}
finally
{
   cmd.Dispose();
   cnn.Dispose();
}
any objects declared within the using scope will be deallocated upon exiting the block.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top