datatable与实体类之间相互转化的几种方法
程序员文章站
2022-03-20 12:26:55
来源:CSDN 原文:https://blog.csdn.net/a11112244444/article/details/78921200 版权声明:本文为博主原创文章,转载请附上博文链接! ......
#region datatable转换成实体类
/// <summary>
/// 填充对象列表:用dataset的第一个表填充实体类
/// </summary>
/// <param name="ds">dataset</param>
/// <returns></returns>
public list<t> fillmodel(dataset ds)
{
if (ds == null || ds.tables[0] == null || ds.tables[0].rows.count == 0)
{
return null;
}
else
{
return fillmodel(ds.tables[0]);
}
}
/// <summary>
/// 填充对象列表:用dataset的第index个表填充实体类
/// </summary>
public list<t> fillmodel(dataset ds, int index)
{
if (ds == null || ds.tables.count <= index || ds.tables[index].rows.count == 0)
{
return null;
}
else
{
return fillmodel(ds.tables[index]);
}
}
/// <summary>
/// 填充对象列表:用datatable填充实体类
/// </summary>
public list<t> fillmodel(datatable dt)
{
if (dt == null || dt.rows.count == 0)
{
return null;
}
list<t> modellist = new list<t>();
foreach (datarow dr in dt.rows)
{
//t model = (t)activator.createinstance(typeof(t));
t model = new t();
for (int i = 0; i < dr.table.columns.count; i++)
{
propertyinfo propertyinfo = model.gettype().getproperty(dr.table.columns[i].columnname);
if (propertyinfo != null && dr[i] != dbnull.value)
propertyinfo.setvalue(model, dr[i], null);
}
modellist.add(model);
}
return modellist;
}
/// <summary>
/// 填充对象:用datarow填充实体类
/// </summary>
public t fillmodel(datarow dr)
{
if (dr == null)
{
return default(t);
}
//t model = (t)activator.createinstance(typeof(t));
t model = new t();
for (int i = 0; i < dr.table.columns.count; i++)
{
propertyinfo propertyinfo = model.gettype().getproperty(dr.table.columns[i].columnname);
if (propertyinfo != null && dr[i] != dbnull.value)
propertyinfo.setvalue(model, dr[i], null);
}
return model;
}
#endregion
#region 实体类转换成datatable
/// <summary>
/// 实体类转换成dataset
/// </summary>
/// <param name="modellist">实体类列表</param>
/// <returns></returns>
public dataset filldataset(list<t> modellist)
{
if (modellist == null || modellist.count == 0)
{
return null;
}
else
{
dataset ds = new dataset();
ds.tables.add(filldatatable(modellist));
return ds;
}
}
/// <summary>
/// 实体类转换成datatable
/// </summary>
/// <param name="modellist">实体类列表</param>
/// <returns></returns>
public datatable filldatatable(list<t> modellist)
{
if (modellist == null || modellist.count == 0)
{
return null;
}
datatable dt = createdata(modellist[0]);
foreach (t model in modellist)
{
datarow datarow = dt.newrow();
foreach (propertyinfo propertyinfo in typeof(t).getproperties())
{
datarow[propertyinfo.name] = propertyinfo.getvalue(model, null);
}
dt.rows.add(datarow);
}
return dt;
}
/// <summary>
/// 根据实体类得到表结构
/// </summary>
/// <param name="model">实体类</param>
/// <returns></returns>
private datatable createdata(t model)
{
datatable datatable = new datatable(typeof(t).name);
foreach (propertyinfo propertyinfo in typeof(t).getproperties())
{
datatable.columns.add(new datacolumn(propertyinfo.name, propertyinfo.propertytype));
}
return datatable;
}
#endregion
---------------------
来源:csdn
原文:https://blog.csdn.net/a11112244444/article/details/78921200
版权声明:本文为博主原创文章,转载请附上博文链接!
上一篇: 一半的东西