Grant Killian's Blog

Sponsors

The Lounge

Wicked Cool Jobs

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
DataReader to DataTable sample code

Carlos, in a comment on my blog, requested the code I used in this post to efficiently convert a DataReader into a DataTable (it's inspired by Roy Osherove's post here and the comments to that post).  For posterity, I'm including the following example of how to extend the base DBDataAdapter class for fun and profit . . . I've formatted the most important code in bold.  This was a useful intermediary between a data access layer that delivered DataReaders and a business layer that worked with DataTables and DataSets.

//CustomAdapter Class
using System;
using System.Data;

namespace TheApplication.DataObjects
{
 public class CustomAdapter : System.Data.Common.DbDataAdapter
 {
  public int FillFromReader(DataTable dataTable, IDataReader dataReader)
  {
   return this.Fill( dataTable, dataReader );
  }

  protected override System.Data.Common.RowUpdatedEventArgs CreateRowUpdatedEvent( DataRow a, IDbCommand b, StatementType c, System.Data.Common.DataTableMapping d )
  {
   return ( System.Data.Common.RowUpdatedEventArgs )new EventArgs();
  }
    
  protected override System.Data.Common.RowUpdatingEventArgs CreateRowUpdatingEvent( DataRow a, IDbCommand b, StatementType c, System.Data.Common.DataTableMapping d )
  {
   return ( System.Data.Common.RowUpdatingEventArgs )new EventArgs();
  }
  
  protected override void OnRowUpdated( System.Data.Common.RowUpdatedEventArgs value )
  {

  }
  protected override void OnRowUpdating( System.Data.Common.RowUpdatingEventArgs value )
  {

  } 
 }
}

And here is a plain vanilla sample usage of the class:

public static DataTable getDataTable( string sql )
  {
   DataTable dtOut = new DataTable();
   OdbcConnection cn = new OdbcConnection( ConnString() );
   cn.Open();
   OdbcCommand cmd = new OdbcCommand( sql, cn );
   IDataReader dr = cmd.ExecuteReader();

   CustomAdapter da = new CustomAdapter();
   da.FillFromReader( dtOut, dr ); //converts a datareader into a datatable
   cn.Close();
   return dtOut;
  }

Happy .Netting!


Posted Tue, Mar 23 2004 12:06 PM by grant.killian

[Advertisement]

Comments

Darrell wrote re: DataReader to DataTable sample code
on Tue, Mar 23 2004 8:33 AM
Nice. Don't know when I'll need it, but it's there!
Drew wrote re: DataReader to DataTable sample code
on Wed, May 26 2004 9:11 AM
This was perfect. Works like a charm. Thanks Grant
Zahid Rasool wrote re: DataReader to DataTable sample code
on Thu, Feb 15 2007 11:35 AM

it simply cool

Bill Roberts wrote re: DataReader to DataTable sample code
on Tue, May 1 2007 5:26 PM

Big-o problemo... How about DISPOSING of the object?  I've converted code to VB and I get INVALID CAST operation on DISPOSE.

ANANDA BABU KARTHIKEYAN wrote re: DataReader to DataTable sample code
on Wed, Jan 9 2008 3:47 AM

Instead of doing this you can use following code

Dim dt as DataTable

Dim dr as SqlDataReader = ExecuteReader()

dt.Fill(dr)

John wrote re: DataReader to DataTable sample code
on Tue, Mar 11 2008 2:01 PM

Actually, the datatable doesn't have a fill, but you can use the "load" method to load a datareaders (of those that implement iDataReader).

dt.load(dr)

chiaty wrote re: DataReader to DataTable sample code
on Tue, Apr 8 2008 8:07 AM

john is right..

make sure you create a new dt obj,

Dim dt as NEW DataTable

Dim dr as SqlDataReader = ExecuteReader()

dt.load(dr)

Adrian Hedley wrote re: DataReader to DataTable sample code
on Tue, Oct 7 2008 9:02 AM

I like the way how the solution was implemented however the DataTable class has a property .Load(IDataReader) that does just that.  That should work for all classes implemnting the IDatareader Interface, including SqlDataReader.

Not a very intuitive method name and unfortunately I found it after I applied Grant's Solution.

Keep up the good work.

Devlicio.us