Cleaning up after using an OleDbDataReader

Posted on 2006-07-18
Last Modified: 2012-06-27

I have just been made aware that I need to call Dispose() when using an OleDbCommand.  I now do this in the "finally" block of the code to make sure it gets called when an exception is thrown.  But what about an OleDbDataReader?

Is this code appropriate?

OleDbDataReader driverRuns = dbSelectCommand1.ExecuteReader();             

If there is an exception in ExecuteReader(), does the Close() need to be called? I assume it's null anyway.  Then what about explicitly setting the OleDbDataReader to null?  This would be something I would also put into the finally block.

driverRuns = null;

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
  • 2
  • 2
LVL 18

Accepted Solution

Ravi Singh earned 300 total points
ID: 17131827
Hi Bob, if all your database related code is within one try catch block then its always good to do a null check on the object in the finally clause (to avoid any runtime null reference exceptions)... if not null then close the reader and dispose it explicitly... this should mark it for garbage collection.

OleDbDataReader driverRuns = null;

     driverRuns = dbSelectCommand1.ExecuteReader();
     //... use the reader
catch (Exception ex)
     if (driverRuns != null)
LVL 25

Assisted Solution

dstanley9 earned 200 total points
ID: 17131865
Easiest way to handle this is to use a using block:

using(OleDbDataReader driverRuns = dbSelectCommand1.ExecuteReader())

This automatically disposes the OleDbDataReader (which closes it) in a finally block without having to explicitly close or dispose of the reader.  Using blocks can be used around Commands and Connections as well.

Author Comment

ID: 17131972
I will use the "using" statement in the future.  But I have hundreds of uses of these objects and need a quick way to make them as safe as possible, and to prevent memory leaks.

So I like Zephyr's approach for now.  Except I don't find a Dispose() in the OleDbDataReader object.

Am I missing something?

LVL 18

Expert Comment

by:Ravi Singh
ID: 17132113
Hi, sorry the data reader doesnt expose the Dipose() method... calling close on the reader should do it

Author Comment

ID: 17132560
Ok.  No problem.


Featured Post

Industry Leaders: 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

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL ( and MongoDB (…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

728 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