Convert AdoRecordSet to DataTable

This forum is the place to discuss issues related to ReportPro, Xs2Ado, Vo2Ado, bBrowser and other 3rd party products
Post Reply
Posts: 85
Joined: Wed Jan 23, 2019 7:54 pm
Location: Germany

Convert AdoRecordSet to DataTable

Post by alex_schmitt »

Hi Robert,

is there a straightforward way to convert an AdoRecordSet into a DataTable?

Background: I have implemented a function to export a data set to XLS in C#. Of course the types AdoRecordSet from VO2Ado and .Net DataTables are not compatible. any better best practice?

Thanks and best,
User avatar
Posts: 4279
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Re: Convert AdoRecordSet to DataTable

Post by robert »

There is no built in way to do this.
You would have to create a DataTable and its columns and add rows for each of the rows in the recordset.
I would probably use GetRows() to get all rows in the RecordSet and then use the ItemArray property of the DataRow to set all field values in one assignment.
The X# runtime has a function _ArrayToObjectArray() that you can use to convert each row returned from GetRows() to an object[] that you can assign to the ItemArray.
The code would somewhat like this (no error handling)

Code: Select all

Function AdoRecordSetToDataTable(oRs as AdoRecordSet) as DataTable
var oDT := DataTable()
foreach oField as AdoField in oRs:Fields
     var oValue    := (OBJECT) oField:Value
     var oColumn := DataColumn{oField:Name, oValue:GetType()}
var aRows := oRs:GetRows()
foreach aRow as Array in aRows
     var oRow:= oDt:NewRow()
     oRow:ItemArray := _ArrayToObjectArray(aRow)
return oDT
If you have a working version, please share that here.

XSharp Development Team
The Netherlands
Post Reply