Home > DeveloperSection > Forums > how use the dispose method for close the connection
Rajesh Goswami

Total Post:29

Points:203
Posted on    November-10-2010 12:46 AM

 ADO.Net ADO.Net 
Ratings:


 4 Reply(s)
 1760  View(s)
Rate this:
explain it


yash Singh
yash Singh

Total Post:15

Points:105
Posted on    December-18-2010 6:26 AM

Dispose() method permanently removes connection object from memory and the resource no longer exists for any further          processing.


string constring = "Server=(local);Database=my; User Id=sa; Password=sa";
SqlConnection sqlcon = new SqlConnection(constring);
 sqlcon.Open();   // connection is open

try
{
//  code here which will be execute
}
catch
{
     // code  will be execute when  error occurred in try block
}
finally
{
 sqlcon.Dispose();         // close and desroy the  connection object
}


Modified On Dec-18-2010 06:28:01 AM

Shankar M

Total Post:25

Points:175
Posted on    February-25-2013 9:14 PM

Hi Rajesh,

You don't need to explicitly close the connection that is opened, You can use the Using Clause.

using(SqlConnection sqlcon = new SqlConnection(constring))
  {

   }

The leverage of using it is the resources tied up to it are freed automatically with out explicitly calling the
Dispose() Method.

Thanks,
Shankar

Modified On Feb-25-2013 09:18:25 PM

Shankar M

Total Post:25

Points:175
Posted on    February-25-2013 9:14 PM

Hi Rajesh,

You don't need to explicitly close the connection that is opened, You can use the Using Clause.

using(SqlConnection sqlcon = new SqlConnection(constring))
  {

   }

The leverage of using it is the resources tied up to it are freed automatically with out explicitly calling the
Dispose() Method.

Thanks,
Shankar

Shankar M

Total Post:25

Points:175
Posted on    February-25-2013 9:16 PM

Post is removed by the User.

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

Follow MindStick