Home > DeveloperSection > Blogs > Difference Between Close() and Dispose() Method.

Difference Between Close() and Dispose() Method.


C# C# 
Ratings:
0 Comment(s)
 6485  View(s)
Rate this:


Difference Between Close() and Dispose() Method.


The basic difference between Close() and Dispose() is, when a Close() method is called, connection will be temporarily closed and can be opened once again. Where as Dispose() method permanently close and removes connection object from memory and the resource no longer exists for any further processing.

Example:

try
{

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


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


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

Follow MindStick