Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
con.dispose()
con.close()

here con is a obj of sqlconnection
Posted
Updated 3-Oct-11 22:52pm
v2
Comments
Dalek Dave 4-Oct-11 4:52am    
Edited title to show it in English, not txtspk, we are not morons.

* If you use connection object one time, use Dispose.
* If connection object must be reused, use Close method.


The basic difference between Close() and Dispose() is, when a Close() method is called, any managed resource can be temporarily closed and can be opened once again. It means that, with the same object the resource can be reopened or used. Where as Dispose() method permanently removes any resource ((un)managed) from memory for cleanup and the resource no longer exists for any further processing.

it's a good practice to provide Close method where suitable
 
Share this answer
 
v3
Comments
Dalek Dave 4-Oct-11 4:53am    
Nice.
member60 24-Oct-11 5:51am    
Thank u Dalek
What is the difference between Close and Dispose?

There is one major difference between calling the Close and Dispose methods on database connections. Close leaves the connection in a closed state; but, it is reusable—all properties, etc. can be accessed and Open can be called. On the other hand, after calling Dispose on a database connection—as with any object—, the connection object can no longer be accessed.

However, calling Dispose does not remove the connection from the connection pool.
Visual C# Best Practice

Only call the Close method on a Stream or a database connection if the object will be reused. Otherwise, use the Dispose method.

The Dispose method may be called on any instance of any type implementing the IDisposable interface. This is supported by the C# using statement which makes calling Dispose automatically easy.


Click[^]
 
Share this answer
 
Comments
Dalek Dave 4-Oct-11 4:52am    
Good answer
Depends on what you want to do..

C#
con.Dispose();
disposes the object and fress the memory occupied

C#
con.Close();

Closes the connection

I guess you just need to close the connection, don't worry about disposing it. Or you may like to create an object every time and dispose it everytime.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900