Do I need to close an OleDbCommand?

Posted on 2006-07-17
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 12

Accepted Solution

deanvanrooyen earned 800 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 600 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.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

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

Assisted Solution

BlackTigerX earned 600 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 http://davidhayden.com/blog/dave/archive/2005/01/13/773.aspx 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month10 days, 2 hours left to enroll

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