每次DataReader里取得数据一个个赋给实体类着实麻烦
写了个方法,用反射牺牲点性能,图个方便了.性能可以用缓存弥补回来

public static object DataReaderToEntity(IDataReader dr, object entity)
{
try
{
PropertyInfo[] pilist = entity.GetType().GetProperties();
foreach (PropertyInfo pi in pilist)
{
for (int i = 0; i < dr.FieldCount; i++)
{
if (pi.Name.Equals(dr.GetName(i)))
{
pi.SetValue(entity, Convert.ChangeType(dr[i], pi.PropertyType), null);
}
}
}
return entity;
}
catch
{
return null;
}
}

用法:
Entity entity = (Entity)DataReaderToEntity(dr, new Entity());

Back