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,
Alex
Convert AdoRecordSet to DataTable
-
- Posts: 85
- Joined: Wed Jan 23, 2019 7:54 pm
- Location: Germany
Re: Convert AdoRecordSet to DataTable
Alex,
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)
If you have a working version, please share that here.
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()}
oDT:Columns:Add(oColumn)
next
var aRows := oRs:GetRows()
foreach aRow as Array in aRows
var oRow:= oDt:NewRow()
oRow:ItemArray := _ArrayToObjectArray(aRow)
oDt:Tows:Add(oRow)
next
return oDT
Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
The Netherlands
robert@xsharp.eu
Re: Convert AdoRecordSet to DataTable
Hi Robert,
In your example, on this line:
I get this error:
Error XS1579 foreach statement cannot operate on variables of type 'Xs2Ado.AdoFields' because 'Xs2Ado.AdoFields' does not contain a public instance or extension definition for 'GetEnumerator'
How do I fix that? oRS:Fields is of class Xs2Ado.AdoFields which is apparently a collection of Xs2Ado.AdoField (without the s) objects.
Kees.
In your example, on this line:
Code: Select all
foreach oField as AdoField in oRs:Fields
Error XS1579 foreach statement cannot operate on variables of type 'Xs2Ado.AdoFields' because 'Xs2Ado.AdoFields' does not contain a public instance or extension definition for 'GetEnumerator'
How do I fix that? oRS:Fields is of class Xs2Ado.AdoFields which is apparently a collection of Xs2Ado.AdoField (without the s) objects.
Kees.
Re: Convert AdoRecordSet to DataTable
XSharp Development Team
The Netherlands
robert@xsharp.eu
The Netherlands
robert@xsharp.eu
Re: Convert AdoRecordSet to DataTable
Hi,
What about using an AdoServer object to fill a System.Data.DataTable object? I am trying to populate a Winforms DataGridView with data from an AdoServer object. For ADS it is like this: create an AdsDataReader object and a System.Data.DataTable object, then do DataTable:Load(AdsDataReader) and DataGridView:DataSource := DataTable. How can I do a similar thing with AdoServer?
Kees.
What about using an AdoServer object to fill a System.Data.DataTable object? I am trying to populate a Winforms DataGridView with data from an AdoServer object. For ADS it is like this: create an AdsDataReader object and a System.Data.DataTable object, then do DataTable:Load(AdsDataReader) and DataGridView:DataSource := DataTable. How can I do a similar thing with AdoServer?
Kees.
Re: Convert AdoRecordSet to DataTable
Kees,
If you want an Ado.Net DataTable then it makes much more sense to also use "plain" Ado.Net to fetch the data and not Xs2Ado.
Which database are you connecting to and which OleDb provider are you using with Xs2Ado?
Robert
If you want an Ado.Net DataTable then it makes much more sense to also use "plain" Ado.Net to fetch the data and not Xs2Ado.
Which database are you connecting to and which OleDb provider are you using with Xs2Ado?
Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
The Netherlands
robert@xsharp.eu
Re: Convert AdoRecordSet to DataTable
Robert,
The database I am connecting to is Microsoft SQL Server and the ODBC driver is (Microsoft) "SQL Server" (SQLSRV32.DLL). Thank you for your help, can you also explain a little bit about why using plain Ado is better than Xs2Ado in this case?
Kees.
Re: Convert AdoRecordSet to DataTable
Kees,
DataTable was designed to work with Ado.Net
I personally would not use ODBC for Ado.Net, but the native SqlClient.
However, this should work (untested)
Instead of OdbcDataAdapter you can also use SqlDataAdapter from the System.Data.SqlClient namespace.
The connection string needs to be a bit different then, since you're not using ODBC.
But the performance should be better using that SqlDataAdapter
Robert
DataTable was designed to work with Ado.Net
I personally would not use ODBC for Ado.Net, but the native SqlClient.
However, this should work (untested)
Code: Select all
using System.Data.Odbc
using System.Data
.
.
var oDa := OdbcDataAdapter{cSelectStatement, cConnectionString}
var oDt := DataTable{}
oDa:Fill(dt)
The connection string needs to be a bit different then, since you're not using ODBC.
But the performance should be better using that SqlDataAdapter
Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
The Netherlands
robert@xsharp.eu