ADODB To DataTable

Have you ever been working with ADODB, specifically ADODB RecordSets.  Have you ever thought to yourself, “There has got to be an easier way!”    Well, take solace because there is.

It is possible through a little bit of code magic, to convert and ADODB RecordSet into a much more manageable and easy to use System.Data.DataTable object.

ADODB.Recordset rs = ReadDatabaseAndGetRecordSet();//Convert the recordset to a datatable
DataTable tbl = new DataTable();
if (rs != null && rs.RecordCount > 0)
{
rs.MoveFirst();
new OleDbDataAdapter().Fill(tbl, rs);
rs.Close();
}
//Now the DataTable is populated and ready to use.

It’s just that simple!  Now you are free to perform reads from the DataTable rather than having to loop through the RecordSet.  This is especially handy when working against COM code that returns values in ADODB RecordSets.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s