Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Do I need to close an OleDbCommand?

Posted on 2006-07-17
9
Medium Priority
?
511 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
[X]
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
9 Comments
 
LVL 12

Accepted Solution

by:
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....

0
 

Author Comment

by:ba272
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?
0
 
LVL 14

Assisted Solution

by:jjardine
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

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

Assisted Solution

by:BlackTigerX
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
0
 
LVL 13

Expert Comment

by:BlackTigerX
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

http://ebersys.blogspot.com
0
 

Author Comment

by:ba272
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.

Thanks.
0
 

Author Comment

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

Expert Comment

by:BlackTigerX
ID: 17135280
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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!
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

610 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