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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dispose or Close\Nothing????

Status
Not open for further replies.

FRANDAZZO

Programmer
Nov 29, 2000
60
0
0
US
Question -

What is the difference between destroying a db conn object by using the dispose method or destroying it by using the following code:

db.close
db = nothing

Also, does it make a difference???

Thanks in advance,
Frandazzo
 
If the object has a .close method, use it.

If the object implements IDisposable, call .Dispose if it's declared as public. Sometimes .Dispose is marked as protected (internal) and you can't call it, but make the effort if you can.

The difference is objects that contain unmanaged resources (network connections, file handles, database connections) typically implement IDisposable in order to release those unmanaged resources. Failing to call .Dispose results in the resource not being released until the next garbage collection cycle. In the case of things like Database connections, where you may be paying license fees on the number of active connections, that can get expensive. Or... other parts of your code will break because they can't gain access to a resource. A common error is not to dispose of file handles -- if you try and re-open the file, it will fail because .NET hasn't really closed it yet (GC cycle hasn't run yet).

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top