Solved

Cleaning up after using an OleDbDataReader

Posted on 2006-07-18
5
463 Views
Last Modified: 2012-06-27
Hi,

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();             
driverRuns.Close();

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;

Thanks,
Bob
0
Comment
Question by:ba272
  • 2
  • 2
5 Comments
 
LVL 18

Accepted Solution

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

try
{
     //...
     driverRuns = dbSelectCommand1.ExecuteReader();
     //... use the reader
}
catch (Exception ex)
{
}
finally
{
     if (driverRuns != null)
     {
          driverRuns.Close();
          driverRuns.Dispose();
     }
}
0
 
LVL 25

Assisted Solution

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

using(OleDbDataReader driverRuns = dbSelectCommand1.ExecuteReader())
{
  while(driverRuns.Read())
  {
   
  }
}

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.
0
 

Author Comment

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

Bob
0
 
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
0
 

Author Comment

by:ba272
ID: 17132560
Ok.  No problem.

Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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!
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

18 Experts available now in Live!

Get 1:1 Help Now