using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Reflection; using Ant.Frame; namespace Ant.ORM { public class DataToEntity { #region 将DataTable转换成List列表 /// /// 返回一条实体数据 /// /// 实体 /// 表数据 /// public static object DataTableToEntity(DataTable dataTable) { Dictionary vl = new Dictionary(); T obj = Activator.CreateInstance(); if (dataTable.Rows.Count > 0) { DataRow row = dataTable.Rows[0]; foreach (PropertyInfo pinfo in obj.GetType().GetProperties()) { DataFieldColumnAttribute dfa = Attribute.GetCustomAttribute(pinfo, typeof(DataFieldColumnAttribute)) as DataFieldColumnAttribute; if (!object.Equals(dfa, null)) { if (!Convert.IsDBNull(row[pinfo.Name])) { try { vl = SetChanaged(pinfo.Name, row[pinfo.Name], vl); pinfo.SetValue(obj, row[pinfo.Name], null); } catch (Exception ex) { pinfo.SetValue(obj, null, null); } } } if (pinfo.Name == "PersistType") { pinfo.SetValue(obj, EntityPersistType.Update, null); } if (pinfo.Name == "Values") { pinfo.SetValue(obj, vl, null); } } } return obj; } /// /// 返回一条实体数据 /// /// 实体 /// 表数据 /// private static object DataTableToEntity(DataSet ds) { Dictionary vl = new Dictionary(); T obj = Activator.CreateInstance(); if (ds.Tables.Count > 0) { if (ds.Tables[0].Rows.Count > 0) { DataRow row = ds.Tables[0].Rows[0]; foreach (PropertyInfo pinfo in obj.GetType().GetProperties()) { DataFieldColumnAttribute dfa = Attribute.GetCustomAttribute(pinfo, typeof(DataFieldColumnAttribute)) as DataFieldColumnAttribute; if (!object.Equals(dfa, null)) { if (!Convert.IsDBNull(row[pinfo.Name])) { try { vl = SetChanaged(pinfo.Name, row[pinfo.Name], vl); pinfo.SetValue(obj, row[pinfo.Name], null); } catch (Exception ex) { pinfo.SetValue(obj, null, null); } } } if (pinfo.Name == "PersistType") { pinfo.SetValue(obj, EntityPersistType.Update, null); } if (pinfo.Name == "Values") { pinfo.SetValue(obj, vl, null); } } } } return obj; } /// /// 返回实体集合 /// /// 实体 /// 表数据 /// private static object DataTableToList(DataTable dataTable) { List list = new List(); Dictionary vl = new Dictionary(); try { foreach (DataRow row in dataTable.Rows) { T obj = Activator.CreateInstance(); foreach (PropertyInfo pinfo in obj.GetType().GetProperties()) { DataFieldColumnAttribute dfa = Attribute.GetCustomAttribute(pinfo, typeof(DataFieldColumnAttribute)) as DataFieldColumnAttribute; if (!object.Equals(dfa, null)) { try { if (!Convert.IsDBNull(row[pinfo.Name])) { vl = SetChanaged(pinfo.Name, row[pinfo.Name], vl); pinfo.SetValue(obj, row[pinfo.Name], null); } else pinfo.SetValue(obj, null, null); } catch (Exception ex) { pinfo.SetValue(obj, null, null); } } if (pinfo.Name == "PersistType") { pinfo.SetValue(obj, EntityPersistType.Update, null); } if (pinfo.Name == "Values") { pinfo.SetValue(obj, vl, null); } } list.Add(obj); } } catch (Exception ex) { } return list; } /// /// 将表数据转换成实体 /// /// 表数据 /// 实体 /// private static object DataTableToList(DataTable dt, object enty) { List list = new List(); if (dt == null) return list; object objj = Activator.CreateInstance(enty.GetType()); foreach (DataRow row in dt.Rows) { foreach (PropertyInfo pinfo in enty.GetType().GetProperties()) { DataFieldColumnAttribute dfa = Attribute.GetCustomAttribute(pinfo, typeof(DataFieldColumnAttribute)) as DataFieldColumnAttribute; if (!object.Equals(dfa, null)) { try { if (!Convert.IsDBNull(row[pinfo.Name])) { pinfo.SetValue(objj, row[pinfo.Name], null); } else { pinfo.SetValue(objj, null, null); } } catch (Exception ex) { pinfo.SetValue(objj, null, null); } if (pinfo.Name == "PersistType") { pinfo.SetValue(objj, EntityPersistType.Update, null); } } } list.Add(objj); } return list; } /// /// 这样也可以不过entity每次的值还在,缺点就是无法清除原先的值 /// /// /// /// /// public static object DataTable2List(IDataReader dr, object enty) { object objj = Activator.CreateInstance(enty.GetType()); if (dr == null) return objj; PropertyInfo[] propertys = enty.GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { DataTable MyDataTable = dr.GetSchemaTable(); foreach (DataRow row in MyDataTable.Rows) { foreach (DataColumn col in MyDataTable.Columns) { #region 属性与字段名称一致的进行赋值 if (pi.Name.ToLower().Equals(row[0].ToString().ToLower())) { try { if (!Convert.IsDBNull(dr[row[0].ToString()])) { pi.SetValue(objj, dr[pi.Name], null); } else pi.SetValue(objj, null, null);//为空,但不为Null } catch (Exception ex) { pi.SetValue(objj, null, null);//为空,但不为Null } } #endregion } if (pi.Name == "PersistType") { pi.SetValue(objj, EntityPersistType.Update, null); } } } return objj; } /// /// 获取一条实体 /// /// /// /// /// public static object DataTableToEntity(DataTable dt, object enty) { object objj = Activator.CreateInstance(enty.GetType()); if (dt == null) return objj; if (dt.Rows.Count == 0) return objj; PropertyInfo[] propertys = enty.GetType().GetProperties(); foreach (DataRow dr in dt.Rows) { foreach (PropertyInfo pi in propertys) { #region 属性与字段名称一致的进行赋值 if (dt.Columns.Contains(pi.Name)) { try { if (!Convert.IsDBNull(dr[pi.Name])) pi.SetValue(objj, dr[pi.Name], null); else pi.SetValue(objj, null, null);//为空,但不为Null } catch (Exception ex) { pi.SetValue(objj, null, null);//为空,但不为Null } } if (pi.Name == "PersistType") { pi.SetValue(objj, EntityPersistType.Update, null); } #endregion } } return objj; } /// /// 这样也可以不过entity每次的值还在,缺点就是无法清除原先的值 /// /// /// /// /// public static object DataTable2List(DataTable dt, object enty) { List list = new List(); if (dt == null) return list; PropertyInfo[] propertys = enty.GetType().GetProperties(); foreach (DataRow dr in dt.Rows) { object objj = Activator.CreateInstance(enty.GetType()); foreach (PropertyInfo pi in propertys) { #region 属性与字段名称一致的进行赋值 if (dt.Columns.Contains(pi.Name)) { try { if (!Convert.IsDBNull(dr[pi.Name])) pi.SetValue(objj, dt.Rows[0][pi.Name], null); else pi.SetValue(objj, null, null);//为空,但不为Null } catch (Exception ex) { pi.SetValue(objj, null, null);//为空,但不为Null } } if (pi.Name == "PersistType") { pi.SetValue(objj, EntityPersistType.Update, null); } #endregion } list.Add(objj); } return list; } /// /// 将DataTable转换为list(用的FOR赋值,效果一样,这种比较复杂) /// /// /// /// public static object DataTable2List(IDataReader dr) { T obj = (T)Activator.CreateInstance(typeof(T)); if (dr == null) return obj; PropertyInfo[] propertys = obj.GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { DataTable MyDataTable = dr.GetSchemaTable(); foreach (DataRow row in MyDataTable.Rows) { foreach (DataColumn col in MyDataTable.Columns) { #region 属性与字段名称一致的进行赋值 if (pi.Name.ToLower().Equals(row[0].ToString().ToLower())) { if (!Convert.IsDBNull(dr[row[0].ToString()])) { try { pi.SetValue(obj, dr[pi.Name], null); } catch (Exception ex) { pi.SetValue(obj, null, null);//为空,但不为Null } } else pi.SetValue(obj, null, null);//为空,但不为Null } #endregion } } if (pi.Name == "PersistType") { pi.SetValue(obj, EntityPersistType.Update, null); } } return obj; } /// /// 将DataTable转换为list(用的FOR赋值,效果一样,这种比较复杂) /// /// /// /// public static List DataTable2List(DataTable dt) { List result = new List(); if (dt == null) return result; foreach (DataRow dr in dt.Rows) { T objj = (T)Activator.CreateInstance(typeof(T)); PropertyInfo[] propertys = objj.GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { #region 属性与字段名称一致的进行赋值 if (dt.Columns.Contains(pi.Name)) { try { if (!Convert.IsDBNull(dr[pi.Name])) pi.SetValue(objj, dr[pi.Name], null); else pi.SetValue(objj, null, null);//为空,但不为Null } catch (Exception ex) { pi.SetValue(objj, null, null);//为空,但不为Null } } if (pi.Name == "PersistType") { pi.SetValue(objj, EntityPersistType.Update, null); } #endregion } result.Add(objj); } return result; } #endregion /// /// 设置初始值 /// /// 字段名称 /// 默认值 private static Dictionary SetChanaged(string FieldName, object defaultValue, Dictionary Values) { if (!Values.ContainsKey(FieldName)) { EntityValue val = new EntityValue(); val.OriginalValue = defaultValue; val.FieldName = FieldName; val.IsChanage = false; Values.Add(FieldName, val); } else { Values[FieldName].OriginalValue = defaultValue; Values[FieldName].IsChanage = false; } return Values; } } }