//下面这个方法是将RecordSet转换为DataTable,其实也没啥技术含量,供大家参考.
//将DataTable转换为RecordSet意思是一样的,因为项目中没有用到,我也就没写.
private static DataTable ConvertRecordsetToDataTable(Recordset recordSet) {
DataTable t = new DataTable(); if (!recordSet.BOF && !recordSet.EOF) {
DataColumn dc;
DataRow dr = null;
string columnName = string.Empty; ADODB.Field field; Type type = null; object value;
recordSet.MoveFirst();
//架构
//name type value
for (int i = 0; i < recordSet.Fields.Count; i++) {
field = recordSet.Fields[i];
columnName = field.Name; //dr = t.NewRow(); switch (field.Type) {
case DataTypeEnum.adArray://与其他类型一起加入逻辑 OR 以指示该数据是那种类型的安全数组
//type = typeof(bool);
throw new Exception(\未知类型\ break;
case DataTypeEnum.adBSTR://以空结尾的字符串 (Unicode)
type = typeof(string); break;
case DataTypeEnum.adBigInt://8字节带符号的整数
type = typeof(Int64); break;
case DataTypeEnum.adBinary://二进制值 //type = typeof(bool);
throw new Exception(\未知类型\ break;
case DataTypeEnum.adBoolean:// 布尔型值 type = typeof(bool); break;
case DataTypeEnum.adChapter: //type = typeof(bool);
throw new Exception(\未知类型\ break;
case DataTypeEnum.adChar://字符串值 type = typeof(char); break;
case DataTypeEnum.adCurrency://货币值,带符号整型值
type = typeof(decimal); break;
case DataTypeEnum.adDBDate://日期值 type = typeof(DateTime); break;
case DataTypeEnum.adDBTime://时间值 type = typeof(DateTime); break;
case DataTypeEnum.adDBTimeStamp://时间戳 (yyyymmddhhmmss 加 10 亿分之一的小数)
type = typeof(DateTime); break;
case DataTypeEnum.adDate://日期按双精度型数值来保存, 数字全部表示从 1899 年 12 月 30 开始的日期数.小数部分是一天当中的片段时间.
type = typeof(DateTime); break;
case DataTypeEnum.adDecimal://具有固定精度和范围的精确数字值
type = typeof(decimal); break;
case DataTypeEnum.adDouble://双精度浮点值 type = typeof(double); break;
case DataTypeEnum.adEmpty://未指定值 //type = typeof(bool);
throw new Exception(\未知类型\ break;
case DataTypeEnum.adError://32 - 位错误代码 //type = typeof(bool);
throw new Exception(\未知类型\ break;
case DataTypeEnum.adFileTime:// //type = typeof(bool);
throw new Exception(\未知类型\ break;
case DataTypeEnum.adGUID:// 全局唯一的标识符 type = typeof(Guid); break;
Idispatch 接口的指针 throw new Exception(\ break;
IUnknown 接口的指针
throw new Exception(\ break;
case DataTypeEnum.adInteger://4 整型
type = typeof(int); break;
值
//type = typeof(bool);
throw new Exception(\ break;
case DataTypeEnum.adLongVarChar:// type = typeof(string); break;
case DataTypeEnum.adLongVarWChar://长字符串值
type = typeof(string); break;
case DataTypeEnum.adNumeric://范围的精确数字值
type = typeof(long); break;
case DataTypeEnum.adPropVariant:
throw new Exception(\ break;
case DataTypeEnum.adSingle:// type = typeof(float);
case DataTypeEnum.adIDispatch://OLE 对象上 未知类型\case DataTypeEnum.adIUnknown://OLE 对象上 未知类型\字节的带符号case DataTypeEnum.adLongVarBinary://长二进制未知类型\长字符串值 以空结尾的具有固定精度和未知类型\单精度浮点值
break;
case DataTypeEnum.adSmallInt://2 字节带符号整型
type = typeof(Int16); break;
case DataTypeEnum.adTinyInt://1 字节带符号整型
type = typeof(sbyte); break;
case DataTypeEnum.adUnsignedBigInt://8 带符号整型
type = typeof(UInt64); break;
符号整型
type = typeof(UInt32); break;
case DataTypeEnum.adUnsignedSmallInt://2 不带符号整型
type = typeof(UInt16); break;
不带符号整型
type = typeof(byte); break;
case DataTypeEnum.adUserDefined:// 变量
//type = typeof(bool);
throw new Exception(\ break;
case DataTypeEnum.adVarBinary:// type = typeof(byte[]);
//throw new Exception(\ break;
case DataTypeEnum.adVarChar:// type = typeof(bool); break;
case DataTypeEnum.adVarNumeric:// type = typeof(long); break;
Unicode 字符串 type = typeof(string); break;
字节不case DataTypeEnum.adUnsignedInt://4 字节不带字节case DataTypeEnum.adUnsignedTinyInt://1 字节用户定义的未知类型\二进制值 未知类型\字符串值 case DataTypeEnum.adVarWChar://以空结尾的
case DataTypeEnum.adVariant://自动变体型 throw new Exception(\未知类型\ break;
case DataTypeEnum.adWChar://以空结尾的 Unicode 字符串
type = typeof(string); break; default: break; }
value = field.Value;
//dr[field.Name] = value;
dc = new DataColumn(columnName, type); t.Columns.Add(dc); //t.Rows.Add(dr); }
//填充表
recordSet.MoveFirst(); while (!recordSet.EOF) {
dr = t.NewRow();
for (int i = 0; i < recordSet.Fields.Count; i++) {
value = recordSet.Fields[i].Value; dr[recordSet.Fields[i].Name] = value; }
t.Rows.Add(dr);
recordSet.MoveNext(); } }
return t; }
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库ADO的RecordSet转换为DataTable在线全文阅读。
相关推荐: