123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589 |
- 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列表
- /// <summary>
- /// 返回一条实体数据
- /// </summary>
- /// <typeparam name="T">实体</typeparam>
- /// <param name="dataTable">表数据</param>
- /// <returns></returns>
- public static object DataTableToEntity<T>(DataTable dataTable)
- {
- Dictionary<string, EntityValue> vl = new Dictionary<string, EntityValue>();
- T obj = Activator.CreateInstance<T>();
- 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;
- }
- /// <summary>
- /// 返回一条实体数据
- /// </summary>
- /// <typeparam name="T">实体</typeparam>
- /// <param name="dataTable">表数据</param>
- /// <returns></returns>
- private static object DataTableToEntity<T>(DataSet ds)
- {
- Dictionary<string, EntityValue> vl = new Dictionary<string, EntityValue>();
- T obj = Activator.CreateInstance<T>();
- 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;
- }
- /// <summary>
- /// 返回实体集合
- /// </summary>
- /// <typeparam name="T">实体</typeparam>
- /// <param name="dataTable">表数据</param>
- /// <returns></returns>
- private static object DataTableToList<T>(DataTable dataTable)
- {
- List<T> list = new List<T>(); Dictionary<string, EntityValue> vl = new Dictionary<string, EntityValue>();
- try
- {
- foreach (DataRow row in dataTable.Rows)
- {
- T obj = Activator.CreateInstance<T>();
- 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;
- }
- /// <summary>
- /// 将表数据转换成实体
- /// </summary>
- /// <param name="dataTable">表数据</param>
- /// <param name="enty">实体</param>
- /// <returns></returns>
- private static object DataTableToList(DataTable dt, object enty)
- {
- List<object> list = new List<object>();
- 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;
- }
- /// <summary>
- /// 这样也可以不过entity每次的值还在,缺点就是无法清除原先的值
- /// </summary>
- /// <param name="dr"></param>
- /// <param name="md"></param>
- /// <param name="enty"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 获取一条实体
- /// </summary>
- /// <param name="dt"></param>
- /// <param name="md"></param>
- /// <param name="enty"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 这样也可以不过entity每次的值还在,缺点就是无法清除原先的值
- /// </summary>
- /// <param name="dr"></param>
- /// <param name="md"></param>
- /// <param name="enty"></param>
- /// <returns></returns>
- public static object DataTable2List(DataTable dt, object enty)
- {
- List<object> list = new List<object>();
- 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;
- }
- /// <summary>
- /// 将DataTable转换为list(用的FOR赋值,效果一样,这种比较复杂)
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="dt"></param>
- /// <returns></returns>
- public static T DataTable2List<T>(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;
- }
- /// <summary>
- /// 将DataTable转换为list(用的FOR赋值,效果一样,这种比较复杂)
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="dt"></param>
- /// <returns></returns>
- public static List<T> DataTable2List<T>(DataTable dt)
- {
- List<T> result = new List<T>();
- 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 备份
- ///// <summary>
- ///// 将DataTable转换为list(用的FOR赋值,效果一样,这种比较复杂)
- ///// </summary>
- ///// <typeparam name="T"></typeparam>
- ///// <param name="dt"></param>
- ///// <returns></returns>
- //public static List<T> DataTable2List<T>(DataTable dt)
- //{
- // List<T> result = new List<T>(); 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;
- //}
- /// <summary>
- /// 暂时不用
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="dr"></param>
- /// <returns></returns>
- //public static object DataTable2List<T>(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
- /// <summary>
- /// 设置初始值
- /// </summary>
- /// <param name="FieldName">字段名称</param>
- /// <param name="defaultValue">默认值</param>
- private static Dictionary<string, EntityValue> SetChanaged(string FieldName, object defaultValue, Dictionary<string, EntityValue> 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;
- }
- }
- }
|