Solved

Do I need to close an OleDbCommand?

Posted on 2006-07-17
9
462 Views
Last Modified: 2008-03-06
Hi,

I just wondered if I need to close this object?  Or is going out of scope enough?

OleDbCommand dbSelectCommand = new OleDbCommand();

Thanks,
Bob
0
Comment
Question by:ba272
9 Comments
 
LVL 12

Accepted Solution

by:
deanvanrooyen earned 200 total points
Comment Utility
hi bob,

theoretically with garbage collection there should be no worries, and by close -  do you mean close the db connection or just set  dbSelectCommand to null....

0
 

Author Comment

by:ba272
Comment Utility
I never thought about setting the dbSelectCommand to null.   Glad I asked.

I call Close() explicitly for connections, but wondered if needed to do anything with dbSelectCommand.

Set it to null then?
0
 
LVL 14

Assisted Solution

by:jjardine
jjardine earned 150 total points
Comment Utility
A good idea is to call the dispose method when you are finished with the object.  It is true that the garbage collector will pick it up but by default it will take 2 trips for the gc to collect it  This is due to the fact that the first sweep by the gc just calls the finalizer.  Then, the next time the gc runs it will collect the garbage.  if you call the dispose method it will call gc.suppressfinalize and the next time the gc runs it will clean up your resources.
0
 

Author Comment

by:ba272
Comment Utility
Do I call Dispose() then set it to null?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 13

Assisted Solution

by:BlackTigerX
BlackTigerX earned 150 total points
Comment Utility
the preferred method is to use "using"

using (OleDbCommand dbSelectCommand = new OleDbCommand()) {
  //use it here
}

that takes care of the cleanup and lets the GC know that you don't need it anymore, it is always recommended that you use "using" for anything that implements IDisposable, such as database classes, reading files, etc
0
 
LVL 13

Expert Comment

by:BlackTigerX
Comment Utility
"using" is like instantiating the object, then using a try..finally and dispose the object at the end

see http://davidhayden.com/blog/dave/archive/2005/01/13/773.aspx for more information

http://ebersys.blogspot.com
0
 

Author Comment

by:ba272
Comment Utility
I make extensive use of a "finally" block where I Close() the connection, so I think to minimize work for myself, I'll add Dispose() into that structure.  My program is large.

Thanks.
0
 

Author Comment

by:ba272
Comment Utility
how about an OleDbDataReader?  Is calling Close() enough?
0
 
LVL 13

Expert Comment

by:BlackTigerX
Comment Utility
the preferred method is to use "using" because it encapsulates the pattern of doing

instantiate object
try {
//work with object
finally {
  Dispose()
}

so it's safer (since you might forget to add the try..finally) and it saves you lines of code
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now