62,067
社区成员
发帖
与我相关
我的任务
分享
using System.Reflection;
using System.Collections;
ArrayList a =new ArrayList();
foreach(DataRow dr in ds.Tables[0].Rows)
{
Detail c =new Detail();//这是我定义的实体类,大家可以换成自己的
foreach (DataColumn dc in dr.Table.Columns)
{
PropertyInfo pi = c.GetType().GetProperty(dc.ColumnName);//获取根据表的类名创建的实体类属性
if(pi!=null)
{
if (dr[dc.ColumnName]!= DBNull.Value)
{
if(pi.PropertyType.IsEnum)
{
pi.SetValue(c,Enum.ToObject(pi.PropertyType,dr[dc.ColumnName]),null);
}
else
{
pi.SetValue(c, dr[dc.ColumnName], null);
}
}
else
{
pi.SetValue(c, null, null);
}
}
a.Add(c);
}