In particular, this article is collated from a network article "mapx read data from the database to form a new layer (C #)"
Implementation of MAPX in C # A new layer is divided into two questions by reading data from the database:
1.MapX reads data from the database to form a new layer;
2. Converts a DataTable into an ADO recordset. The second problem here is caused by the first problem, because MAPX is a COM control, and it only supports ADO data access, while C # programming typically uses the ado.net approach, which requires a conversion between the two ways. (You can also use the ADO method in C #, of course)
The Operation code for a DataTable converted to an ADO recordset is shown below.
<summary>
Using Ado.net in. NET replaces the ADO implementation of data access, but some COM controls support ADO only and do not support ado.net.
In order to use this type of control, only the data objects in the ado.net, such as converting the DataTable to the Recordset in ADO
(The DataSet object is essentially a collection of DataTable, so this article only covers the conversion of the DataTable object.)
</summary>
public sealed class Adonettoado
{
<summary>
Convert a DataTable object to a Recordeset object
</summary>
<param name= "Table" >datatable object </param>
<returns> the Recordeset objects obtained after conversion </returns>
public static Recordset Convertdatatabletorecordset (DataTable table)
{
Ideas:
1. After the Recordset object is created, a field is created in the column corresponding to the DataTable, which requires converting the Ado.net data type to the data type of ADO;
2. Open the Recordset object, corresponding to each row in the DataTable object, create a new record in the Recordset object, and assign a value to each field.
Recordset rs = new RecordsetClass ();
foreach (DataColumn dc in table. Columns)
{
Rs. Fields.Append (DC. ColumnName, Getdatatype (DC. DataType),-1, fieldattributeenum.adfldisnullable, Missing.Value);
}
Rs. Open (Missing.Value, Missing.Value, Cursortypeenum.adopenunspecified, Locktypeenum.adlockunspecified,-1);
foreach (DataRow dr in table.) Rows)
{
Rs. AddNew (Missing.Value, Missing.Value); Object o;
for (int i = 0; i < table. Columns.count; i++)
{
Rs. Fields[i]. Value = Dr[i];
o = Rs. Fields [i]. Value;
}
}
Return RS;
}
<summary>
Convert a ado.net data type to an ADO data type
</summary>
<param name= "DataType" >ado. NET data type </param>
<returns>ado Data Type </returns>
private static DataTypeEnum Getdatatype (Type dataType)
{
Switch (datatype.tostring ())
{
Case "System.Boolean": return Datatypeenum.adboolean;
Case "System.Byte": return datatypeenum.adunsignedtinyint;
Case "System.Char": return Datatypeenum.adchar;
Case "System.DateTime": return datatypeenum.addate;
Case "System.Decimal": return datatypeenum.addecimal;
Case "System.Double": return datatypeenum.addouble;
Case "System.Int16": return datatypeenum.adsmallint;
Case "System.Int32": return Datatypeenum.adinteger;
Case "System.Int64": return datatypeenum.adbigint;
Case "System.SByte": return datatypeenum.adtinyint;
Case "System.Single": return datatypeenum.adsingle;
Case "System.String": return Datatypeenum.advarchar;
Case "TimeSpan": Return DataTypeEnum.
Case "System.UInt16": return datatypeenum.adunsignedsmallint;
Case "System.UInt32": return datatypeenum.adunsignedint;
Case "System.UInt64": return datatypeenum.adunsignedbigint;
Default:throw (New Exception ("No corresponding data type"));
}
}
}