using System; using System.Collections.Generic; using System.Linq; using System.Text; using Ant.ORM; using System.Reflection; using System.Data; using Ant.Frame; namespace Ant.ORM { public static class DataToMod { #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.Contains("PersistType")) { pinfo.SetValue(obj, EntityPersistType.Update, null); } if (pinfo.Name.Contains("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.Contains("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.Contains("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])) if ((Object.Equals(dr[pi.Name], null)) || (Object.Equals(dr[pi.Name], DBNull.Value))) pi.SetValue(objj, null, null);//为空,但不为Null else pi.SetValue(objj, dr[pi.Name], null); } catch (Exception ex) { pi.SetValue(objj, null, null);//为空,但不为Null } } #endregion if (pi.Name.Contains("PersistType")) { pi.SetValue(objj, EntityPersistType.Update, null); } } } 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])) if ((Object.Equals(dr[pi.Name], null)) || (Object.Equals(dr[pi.Name], DBNull.Value))) pi.SetValue(objj, null, null);//为空,但不为Null else pi.SetValue(objj, dt.Rows[0][pi.Name], null); } catch (Exception ex) { pi.SetValue(objj, null, null);//为空,但不为Null } } #endregion if (pi.Name.Contains("PersistType")) { pi.SetValue(objj, EntityPersistType.Update, null); } } list.Add(objj); } return list; } /// /// 将DataTable转换为list(用的FOR赋值,效果一样,这种比较复杂) /// /// /// /// public static T 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())) { try { if (!Convert.IsDBNull(dr[row[0].ToString()])) { pi.SetValue(obj, dr[pi.Name], null); } else pi.SetValue(obj, null, null);//为空,但不为Null } catch (Exception ex) { pi.SetValue(obj, null, null);//为空,但不为Null } } #endregion } } if (pi.Name.Contains("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 currentInfo = objj.GetType().GetProperty("列名"); PropertyInfo[] propertys = objj.GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { #region 属性与字段名称一致的进行赋值 if (dt.Columns.Contains(pi.Name)) { try { //if (!Convert.IsDBNull(dr[pi.Name])) if ((Object.Equals(dr[pi.Name], null)) || (Object.Equals(dr[pi.Name], DBNull.Value))) pi.SetValue(objj, null, null);//为空,但不为Null else pi.SetValue(objj, dr[pi.Name], null); } catch (Exception ex) { pi.SetValue(objj, null, null);//为空,但不为Null } } #endregion if (pi.Name.Contains("PersistType")) { pi.SetValue(objj, EntityPersistType.Update, null); } } result.Add(objj); } return result; } #endregion #region 备份 ///// ///// 将DataTable转换为list(用的FOR赋值,效果一样,这种比较复杂) ///// ///// ///// ///// //public static List DataTable2List(DataTable dt) //{ // List result = new List(); string colname = ""; // MetaData md = MetaDataManager.GetMetaData(typeof(T)); // if (dt == null) return result; // for (int j = 0; j < dt.Rows.Count; j++) // { // T objj = (T)Activator.CreateInstance(typeof(T)); // PropertyInfo[] propertys = objj.GetType().GetProperties(); // foreach (PropertyInfo pi in propertys) // { // colname = ""; // for (int i = 0; i < dt.Columns.Count; i++) // { // if (pi.Name.ToLower().Equals(dt.Columns[i].ColumnName.ToLower())) // { // colname = pi.Name; break; // } // } // if (!string.IsNullOrEmpty(colname)) // { // #region 属性与字段名称一致的进行赋值 // //if (dt.Rows[j][colname] != DBNull.Value && dt.Rows[j][colname] != null && !object.Equals(dt.Rows[j][colname], null)) // if (!Convert.IsDBNull(dt.Rows[j][colname])) // { // try // { // pi.SetValue(objj, dt.Rows[j][md.FieldMeteDatas[colname].ColumnName], null); // } // catch (Exception ex) // { // pi.SetValue(objj, null, null);//为空,但不为Null // // } // } // else // { // pi.SetValue(objj, null, null);//为空,但不为Null // } // #endregion // } // if (pi.Name == "PersistType") // { // pi.SetValue(objj, EntityPersistType.Update, null); // } // } // result.Add(objj); // } // return result; //} /// /// 暂时不用 /// /// /// /// //public static object DataTable2List(IDataReader dr) //{ // T obj = (T)Activator.CreateInstance(typeof(T)); // MetaData md = MetaDataManager.GetMetaData(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 (dr[row[0].ToString()] != DBNull.Value) // //if (dr[row[0].ToString()] != DBNull.Value && dr[row[0].ToString()] != null && !object.Equals(dr[row[0].ToString()], null)) // if (!Convert.IsDBNull(dr[row[0].ToString()])) // { // if (pi.PropertyType.ToString().ToLower().Contains("system.int64")) // { // pi.SetValue(obj, Int64.Parse(dr[row[0].ToString()].ToString()), null); // } // if (pi.PropertyType.ToString().ToLower().Contains("system.int32")) // { // pi.SetValue(obj, Int32.Parse(dr[row[0].ToString()].ToString()), null); // } // else if (pi.PropertyType.ToString().ToLower().Contains("system.datetime")) // { // pi.SetValue(obj, Convert.ToDateTime(dr[row[0].ToString()].ToString()), null); // } // else if (pi.PropertyType.ToString().ToLower().Contains("system.boolean")) // { // pi.SetValue(obj, Convert.ToBoolean(dr[row[0].ToString()].ToString()), null); // } // else if (pi.PropertyType.ToString().ToLower().Contains("system.single")) // { // pi.SetValue(obj, Convert.ToSingle(dr[row[0].ToString()].ToString()), null); // } // else if (pi.PropertyType.ToString().ToLower().Contains("system.double")) // { // pi.SetValue(obj, Convert.ToDouble(dr[row[0].ToString()].ToString()), null); // } // else // { // pi.SetValue(obj, dr[md.FieldMeteDatas[pi.Name].ColumnName], null); // } // } // else // pi.SetValue(obj, null, null);//为空,但不为Null // } // #endregion // } // } // if (pi.Name == "PersistType") // { // pi.SetValue(obj, EntityPersistType.Update, null); // } // } // return obj; //} #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; } } }