CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Grant Killian's Blog

No, this has nothing to do with beer -- but maybe it should?

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!



Comments

Drew said:

This was perfect. Works like a charm. Thanks Grant
# May 26, 2004 9:11 AM

Zahid Rasool said:

it simply cool

# February 15, 2007 11:35 AM

Bill Roberts said:

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

# May 1, 2007 5:26 PM

ANANDA BABU KARTHIKEYAN said:

Instead of doing this you can use following code

Dim dt as DataTable

Dim dr as SqlDataReader = ExecuteReader()

dt.Fill(dr)

# January 9, 2008 3:47 AM

John said:

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)

# March 11, 2008 2:01 PM

chiaty said:

john is right..

make sure you create a new dt obj,

Dim dt as NEW DataTable

Dim dr as SqlDataReader = ExecuteReader()

dt.load(dr)

# April 8, 2008 8:07 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!