转:DataReader填充为List<T>
2009-09-10 09:57
351 查看
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Workflow.Runtime.Tracking;
namespace Test
{
//自定义的Attribute,用于与数据库字段进行绑定
public class BindingFieldAttribute : System.Attribute
{
public string FieldName { get; set; }
}
//通用实体类
public class BaseBusinessObject
{
//....
}
public class Test
{
//BaseBusinessObject为通用数据实体类,此处仅为限定T所继承的类型
public static IList<T> FillDataListGeneric<T>(IDataReader reader) where T : BaseBusinessObject
{
//实例化一个List<>泛型集合
IList<T> DataList = new List<T>();
while (reader.Read())
{
T RowInstance = Activator.CreateInstance<T>();//动态创建数据实体对象
//通过反射取得对象所有的Property
foreach (PropertyInfo Property in typeof(T).GetProperties())
{
foreach (BindingFieldAttribute FieldAttr in Property.GetCustomAttributes(typeof(BindingFieldAttribute), true))
{
try
{
//取得当前数据库字段的顺序
int Ordinal = reader.GetOrdinal(FieldAttr.FieldName);
if (reader.GetValue(Ordinal) != DBNull.Value)
{
//将DataReader读取出来的数据填充到对象实体的属性里
Property.SetValue(RowInstance, Convert.ChangeType(reader.GetValue(Ordinal), Property.PropertyType), null);
}
}
catch
{
break;
}
}
}
DataList.Add(RowInstance);
}
return DataList;
}
}
}
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Workflow.Runtime.Tracking;
namespace Test
{
//自定义的Attribute,用于与数据库字段进行绑定
public class BindingFieldAttribute : System.Attribute
{
public string FieldName { get; set; }
}
//通用实体类
public class BaseBusinessObject
{
//....
}
public class Test
{
//BaseBusinessObject为通用数据实体类,此处仅为限定T所继承的类型
public static IList<T> FillDataListGeneric<T>(IDataReader reader) where T : BaseBusinessObject
{
//实例化一个List<>泛型集合
IList<T> DataList = new List<T>();
while (reader.Read())
{
T RowInstance = Activator.CreateInstance<T>();//动态创建数据实体对象
//通过反射取得对象所有的Property
foreach (PropertyInfo Property in typeof(T).GetProperties())
{
foreach (BindingFieldAttribute FieldAttr in Property.GetCustomAttributes(typeof(BindingFieldAttribute), true))
{
try
{
//取得当前数据库字段的顺序
int Ordinal = reader.GetOrdinal(FieldAttr.FieldName);
if (reader.GetValue(Ordinal) != DBNull.Value)
{
//将DataReader读取出来的数据填充到对象实体的属性里
Property.SetValue(RowInstance, Convert.ChangeType(reader.GetValue(Ordinal), Property.PropertyType), null);
}
}
catch
{
break;
}
}
}
DataList.Add(RowInstance);
}
return DataList;
}
}
}
相关文章推荐
- android 解析webservice List<Object> 填充下拉列表
- DataReader转换为IList<T>
- Hibernate的查询query.list()返回的List<Object[]>,如何填充到POI的cell.setCellValue
- ADF Faces 表格应用基础案例一:应用List<Class>填充文本表格
- .net 讀取DataReader 返回List<T>
- 深入理解 c# 第三章 List<int> 填充整数后计算平方根
- C# 的List<T>筛选数据填充到另一个List<T>
- C# - list<>数据填充到Dataset里
- 类型转换——如何将一个List<Map<String,String>>类型换成json格式
- List<Object>和List<String>
- list<string> to byte[]
- 如何将List<Interger> 转为List<String>
- Java集合归纳-<四>List
- 模式匹配的类型测试不能识别List<’a>
- java list<Map<String,Object>>排序
- C++ error C2662: “LinkNode<T> *List<T>::Locate(int)”: 不能将“this”指针从“const List<int>”转换为“List<int> &”
- 关于 List<Map<String,String>> 排序
- FastJSON 转换List<T> ,Map<T,T>泛型失败 处理方法
- json字符串Map(list<Map>)解析
- List<T> 排序问题分享