将DataTable对象转换为Recordset对象
在.net中用ADO.NET取代了ADO实现对数据的访问,但一些COM控件只支持ADO并不支持ADO.NET。为了使用这类控件,只能将ADO.NET中的数据对象,比如DataTable为ADO中的Recordset(DataSet对象本质上是DataTable的集合,因此本文只讲述DataTable对象的转换)。
思路
1. 创建Recordset对象后,在其中对应DataTable的Column创建Field,为此需要将ADO.NET的数据类型转换为ADO的数据类型;
2. 打开Recordset对象,对应DataTable对象中的每一行,在Recordset对象中新建一条记录,并对每个字段赋值。
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using ADODB;
using System.Text.RegularExpressions;
using System.Reflection;
namespace OWC
{
public class DBA
{
SqlConnection conn;
SqlDataAdapter da;
SqlCommand comm;
DataTable dt = new DataTable();
string connStr = "server=localhost;database=Cases20061227;uid=sa;pwd=sa;";
string selStr = "select * from TABLE1";
public DataTable getdata()
{
conn = new SqlConnection(connStr);
da = new SqlDataAdapter(selStr,conn);
da.Fill(dt);
return dt;
}
public Recordset GetRecordset()
{
ADODB.ConnectionClass connADO = new ConnectionClass();
ADODB.RecordsetClass recordADO = new RecordsetClass();
string connStr = "driver={sql server};server=localhost;uid=sa;pwd=sa; database=Cases20061227";
connADO.Open(connStr, "", "", -1);
recordADO.ActiveConnection = connADO;
recordADO.CursorType = ADODB.CursorTypeEnum.adOpenStatic;
recordADO.CursorLocation = ADODB.CursorLocationEnum.adUseClient;
string strSQL = "select * from TABLE1";
recordADO.Open(strSQL, connADO, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockBatchOptimistic, 1);
return recordADO;
}
//Missing的命名空间using System.Reflection;
public Recordset ConvertDataTableToRecordset(DataTable table)
{
Recordset rs=new RecordsetClass();
foreach(DataColumn dc in table.Columns)
{
rs.Fields._Append(dc.ColumnName, GetDataType(dc.DataType), -1, FieldAttributeEnum.adFldIsNullable);
}
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;
}
public 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 "System.UInt16": return DataTypeEnum.adUnsignedSmallInt;
case "System.UInt32": return DataTypeEnum.adUnsignedInt;
case "System.UInt64": return DataTypeEnum.adUnsignedBigInt;
default: throw new Exception("没有对应的数据类型");
}
}
}
}
说明:
1. 需要添加ADODB库的引用,该库位于COM页,名称为“Microsoft ActiveX Data Objects 2.0 Library”;
2. C#语法中没有默认参数,只能使用System.Reflection.Missing的Value属性,但该属性只能用于引用类型,而不能用于枚举、整型等值类型。