Do I need to close an OleDbCommand?

Posted on 2006-07-17
Last Modified: 2008-03-06

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

OleDbCommand dbSelectCommand = new OleDbCommand();

Question by:ba272
LVL 12

Accepted Solution

deanvanrooyen earned 200 total points
ID: 17127406
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....


Author Comment

ID: 17127413
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?
LVL 14

Assisted Solution

jjardine earned 150 total points
ID: 17127870
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.
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.


Author Comment

ID: 17127880
Do I call Dispose() then set it to null?
LVL 13

Assisted Solution

BlackTigerX earned 150 total points
ID: 17128011
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
LVL 13

Expert Comment

ID: 17128025
"using" is like instantiating the object, then using a try..finally and dispose the object at the end

see for more information

Author Comment

ID: 17130138
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.


Author Comment

ID: 17130265
how about an OleDbDataReader?  Is calling Close() enough?
LVL 13

Expert Comment

ID: 17135280
the preferred method is to use "using" because it encapsulates the pattern of doing

instantiate object
try {
//work with object
finally {

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

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity dynamic formulas in runtime 11 75
Error on link 14 48
Set value of dynamically created checkboxlist 2 34
2 questions 10 25
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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