/// <summary>
/// 给model类自动赋值
/// </summary>
/// <param name="sqlstring">获取查询一个model实例的sql语句</param>
/// <param name="obj">model实例对象</param>
/// <returns></returns>
public object selmodel(string sqlstring,object obj)
{
DataTable dtsell = lcommonbll.GetTable(sqlstring);
int count = dtsell.Rows.Count;
if (count == 0)
{
return null;
}
else
{
DataRow dr = dtsell.Rows[0];
#region 另一种方法
//foreach (DataColumn col in dr.Table.Columns)
//{
// PropertyInfo pt = seller.GetType().GetProperty(col.ColumnName);
// if (String.IsNullOrEmpty(dr[col.ColumnName].ToString()))
// {
// break;
// }
// else
// {
// pt.SetValue(seller, dtsell.Rows[0][0], null);
// }
//}
#endregion
foreach (System.Reflection.PropertyInfo pi in obj.GetType().GetProperties())
{
if (pi.CanWrite)
{
try
{
if (dtsell.Rows[0][pi.Name]!=null)
{
pi.SetValue(obj, dtsell.Rows[0][pi.Name], null);
}
else
{
pi.SetValue(obj, null, null);
}
}
catch
{
pi.SetValue(obj, null, null);
}
}
}
return obj;
}
}
Seller seller = new Seller();//实体类
bind BIND = new bind();//绑定方法类
seller = (Seller)BIND.selmodel("select * from seller where SID=2", seller);//赋值
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有