DataToEntity.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using System.Reflection;
  7. using Ant.Frame;
  8. namespace Ant.ORM
  9. {
  10. public class DataToEntity
  11. {
  12. #region 将DataTable转换成List列表
  13. /// <summary>
  14. /// 返回一条实体数据
  15. /// </summary>
  16. /// <typeparam name="T">实体</typeparam>
  17. /// <param name="dataTable">表数据</param>
  18. /// <returns></returns>
  19. public static object DataTableToEntity<T>(DataTable dataTable)
  20. {
  21. Dictionary<string, EntityValue> vl = new Dictionary<string, EntityValue>();
  22. T obj = Activator.CreateInstance<T>();
  23. if (dataTable.Rows.Count > 0)
  24. {
  25. DataRow row = dataTable.Rows[0];
  26. foreach (PropertyInfo pinfo in obj.GetType().GetProperties())
  27. {
  28. DataFieldColumnAttribute dfa = Attribute.GetCustomAttribute(pinfo, typeof(DataFieldColumnAttribute)) as DataFieldColumnAttribute;
  29. if (!object.Equals(dfa, null))
  30. {
  31. if (!Convert.IsDBNull(row[pinfo.Name]))
  32. {
  33. try
  34. {
  35. vl = SetChanaged(pinfo.Name, row[pinfo.Name], vl);
  36. pinfo.SetValue(obj, row[pinfo.Name], null);
  37. }
  38. catch (Exception ex)
  39. {
  40. pinfo.SetValue(obj, null, null);
  41. }
  42. }
  43. }
  44. if (pinfo.Name == "PersistType")
  45. {
  46. pinfo.SetValue(obj, EntityPersistType.Update, null);
  47. }
  48. if (pinfo.Name == "Values")
  49. {
  50. pinfo.SetValue(obj, vl, null);
  51. }
  52. }
  53. }
  54. return obj;
  55. }
  56. /// <summary>
  57. /// 返回一条实体数据
  58. /// </summary>
  59. /// <typeparam name="T">实体</typeparam>
  60. /// <param name="dataTable">表数据</param>
  61. /// <returns></returns>
  62. private static object DataTableToEntity<T>(DataSet ds)
  63. {
  64. Dictionary<string, EntityValue> vl = new Dictionary<string, EntityValue>();
  65. T obj = Activator.CreateInstance<T>();
  66. if (ds.Tables.Count > 0)
  67. {
  68. if (ds.Tables[0].Rows.Count > 0)
  69. {
  70. DataRow row = ds.Tables[0].Rows[0];
  71. foreach (PropertyInfo pinfo in obj.GetType().GetProperties())
  72. {
  73. DataFieldColumnAttribute dfa = Attribute.GetCustomAttribute(pinfo, typeof(DataFieldColumnAttribute)) as DataFieldColumnAttribute;
  74. if (!object.Equals(dfa, null))
  75. {
  76. if (!Convert.IsDBNull(row[pinfo.Name]))
  77. {
  78. try
  79. {
  80. vl = SetChanaged(pinfo.Name, row[pinfo.Name], vl);
  81. pinfo.SetValue(obj, row[pinfo.Name], null);
  82. }
  83. catch (Exception ex)
  84. {
  85. pinfo.SetValue(obj, null, null);
  86. }
  87. }
  88. }
  89. if (pinfo.Name == "PersistType")
  90. {
  91. pinfo.SetValue(obj, EntityPersistType.Update, null);
  92. }
  93. if (pinfo.Name == "Values")
  94. {
  95. pinfo.SetValue(obj, vl, null);
  96. }
  97. }
  98. }
  99. }
  100. return obj;
  101. }
  102. /// <summary>
  103. /// 返回实体集合
  104. /// </summary>
  105. /// <typeparam name="T">实体</typeparam>
  106. /// <param name="dataTable">表数据</param>
  107. /// <returns></returns>
  108. private static object DataTableToList<T>(DataTable dataTable)
  109. {
  110. List<T> list = new List<T>(); Dictionary<string, EntityValue> vl = new Dictionary<string, EntityValue>();
  111. try
  112. {
  113. foreach (DataRow row in dataTable.Rows)
  114. {
  115. T obj = Activator.CreateInstance<T>();
  116. foreach (PropertyInfo pinfo in obj.GetType().GetProperties())
  117. {
  118. DataFieldColumnAttribute dfa = Attribute.GetCustomAttribute(pinfo, typeof(DataFieldColumnAttribute)) as DataFieldColumnAttribute;
  119. if (!object.Equals(dfa, null))
  120. {
  121. try
  122. {
  123. if (!Convert.IsDBNull(row[pinfo.Name]))
  124. {
  125. vl = SetChanaged(pinfo.Name, row[pinfo.Name], vl);
  126. pinfo.SetValue(obj, row[pinfo.Name], null);
  127. }
  128. else
  129. pinfo.SetValue(obj, null, null);
  130. }
  131. catch (Exception ex)
  132. {
  133. pinfo.SetValue(obj, null, null);
  134. }
  135. }
  136. if (pinfo.Name == "PersistType")
  137. {
  138. pinfo.SetValue(obj, EntityPersistType.Update, null);
  139. }
  140. if (pinfo.Name == "Values")
  141. {
  142. pinfo.SetValue(obj, vl, null);
  143. }
  144. }
  145. list.Add(obj);
  146. }
  147. }
  148. catch (Exception ex)
  149. {
  150. }
  151. return list;
  152. }
  153. /// <summary>
  154. /// 将表数据转换成实体
  155. /// </summary>
  156. /// <param name="dataTable">表数据</param>
  157. /// <param name="enty">实体</param>
  158. /// <returns></returns>
  159. private static object DataTableToList(DataTable dt, object enty)
  160. {
  161. List<object> list = new List<object>();
  162. if (dt == null) return list;
  163. object objj = Activator.CreateInstance(enty.GetType());
  164. foreach (DataRow row in dt.Rows)
  165. {
  166. foreach (PropertyInfo pinfo in enty.GetType().GetProperties())
  167. {
  168. DataFieldColumnAttribute dfa = Attribute.GetCustomAttribute(pinfo, typeof(DataFieldColumnAttribute)) as DataFieldColumnAttribute;
  169. if (!object.Equals(dfa, null))
  170. {
  171. try
  172. {
  173. if (!Convert.IsDBNull(row[pinfo.Name]))
  174. {
  175. pinfo.SetValue(objj, row[pinfo.Name], null);
  176. }
  177. else
  178. {
  179. pinfo.SetValue(objj, null, null);
  180. }
  181. }
  182. catch (Exception ex)
  183. {
  184. pinfo.SetValue(objj, null, null);
  185. }
  186. if (pinfo.Name == "PersistType")
  187. {
  188. pinfo.SetValue(objj, EntityPersistType.Update, null);
  189. }
  190. }
  191. }
  192. list.Add(objj);
  193. }
  194. return list;
  195. }
  196. /// <summary>
  197. /// 这样也可以不过entity每次的值还在,缺点就是无法清除原先的值
  198. /// </summary>
  199. /// <param name="dr"></param>
  200. /// <param name="md"></param>
  201. /// <param name="enty"></param>
  202. /// <returns></returns>
  203. public static object DataTable2List(IDataReader dr, object enty)
  204. {
  205. object objj = Activator.CreateInstance(enty.GetType());
  206. if (dr == null) return objj;
  207. PropertyInfo[] propertys = enty.GetType().GetProperties();
  208. foreach (PropertyInfo pi in propertys)
  209. {
  210. DataTable MyDataTable = dr.GetSchemaTable();
  211. foreach (DataRow row in MyDataTable.Rows)
  212. {
  213. foreach (DataColumn col in MyDataTable.Columns)
  214. {
  215. #region 属性与字段名称一致的进行赋值
  216. if (pi.Name.ToLower().Equals(row[0].ToString().ToLower()))
  217. {
  218. try
  219. {
  220. if (!Convert.IsDBNull(dr[row[0].ToString()]))
  221. {
  222. pi.SetValue(objj, dr[pi.Name], null);
  223. }
  224. else
  225. pi.SetValue(objj, null, null);//为空,但不为Null
  226. }
  227. catch (Exception ex)
  228. {
  229. pi.SetValue(objj, null, null);//为空,但不为Null
  230. }
  231. }
  232. #endregion
  233. }
  234. if (pi.Name == "PersistType")
  235. {
  236. pi.SetValue(objj, EntityPersistType.Update, null);
  237. }
  238. }
  239. }
  240. return objj;
  241. }
  242. /// <summary>
  243. /// 获取一条实体
  244. /// </summary>
  245. /// <param name="dt"></param>
  246. /// <param name="md"></param>
  247. /// <param name="enty"></param>
  248. /// <returns></returns>
  249. public static object DataTableToEntity(DataTable dt, object enty)
  250. {
  251. object objj = Activator.CreateInstance(enty.GetType());
  252. if (dt == null) return objj;
  253. if (dt.Rows.Count == 0) return objj;
  254. PropertyInfo[] propertys = enty.GetType().GetProperties();
  255. foreach (DataRow dr in dt.Rows)
  256. {
  257. foreach (PropertyInfo pi in propertys)
  258. {
  259. #region 属性与字段名称一致的进行赋值
  260. if (dt.Columns.Contains(pi.Name))
  261. {
  262. try
  263. {
  264. if (!Convert.IsDBNull(dr[pi.Name]))
  265. pi.SetValue(objj, dr[pi.Name], null);
  266. else
  267. pi.SetValue(objj, null, null);//为空,但不为Null
  268. }
  269. catch (Exception ex)
  270. {
  271. pi.SetValue(objj, null, null);//为空,但不为Null
  272. }
  273. }
  274. if (pi.Name == "PersistType")
  275. {
  276. pi.SetValue(objj, EntityPersistType.Update, null);
  277. }
  278. #endregion
  279. }
  280. }
  281. return objj;
  282. }
  283. /// <summary>
  284. /// 这样也可以不过entity每次的值还在,缺点就是无法清除原先的值
  285. /// </summary>
  286. /// <param name="dr"></param>
  287. /// <param name="md"></param>
  288. /// <param name="enty"></param>
  289. /// <returns></returns>
  290. public static object DataTable2List(DataTable dt, object enty)
  291. {
  292. List<object> list = new List<object>();
  293. if (dt == null) return list;
  294. PropertyInfo[] propertys = enty.GetType().GetProperties();
  295. foreach (DataRow dr in dt.Rows)
  296. {
  297. object objj = Activator.CreateInstance(enty.GetType());
  298. foreach (PropertyInfo pi in propertys)
  299. {
  300. #region 属性与字段名称一致的进行赋值
  301. if (dt.Columns.Contains(pi.Name))
  302. {
  303. try
  304. {
  305. if (!Convert.IsDBNull(dr[pi.Name]))
  306. pi.SetValue(objj, dt.Rows[0][pi.Name], null);
  307. else
  308. pi.SetValue(objj, null, null);//为空,但不为Null
  309. }
  310. catch (Exception ex)
  311. {
  312. pi.SetValue(objj, null, null);//为空,但不为Null
  313. }
  314. }
  315. if (pi.Name == "PersistType")
  316. {
  317. pi.SetValue(objj, EntityPersistType.Update, null);
  318. }
  319. #endregion
  320. }
  321. list.Add(objj);
  322. }
  323. return list;
  324. }
  325. /// <summary>
  326. /// 将DataTable转换为list(用的FOR赋值,效果一样,这种比较复杂)
  327. /// </summary>
  328. /// <typeparam name="T"></typeparam>
  329. /// <param name="dt"></param>
  330. /// <returns></returns>
  331. public static object DataTable2List<T>(IDataReader dr)
  332. {
  333. T obj = (T)Activator.CreateInstance(typeof(T));
  334. if (dr == null) return obj;
  335. PropertyInfo[] propertys = obj.GetType().GetProperties();
  336. foreach (PropertyInfo pi in propertys)
  337. {
  338. DataTable MyDataTable = dr.GetSchemaTable();
  339. foreach (DataRow row in MyDataTable.Rows)
  340. {
  341. foreach (DataColumn col in MyDataTable.Columns)
  342. {
  343. #region 属性与字段名称一致的进行赋值
  344. if (pi.Name.ToLower().Equals(row[0].ToString().ToLower()))
  345. {
  346. if (!Convert.IsDBNull(dr[row[0].ToString()]))
  347. {
  348. try
  349. {
  350. pi.SetValue(obj, dr[pi.Name], null);
  351. }
  352. catch (Exception ex)
  353. {
  354. pi.SetValue(obj, null, null);//为空,但不为Null
  355. }
  356. }
  357. else
  358. pi.SetValue(obj, null, null);//为空,但不为Null
  359. }
  360. #endregion
  361. }
  362. }
  363. if (pi.Name == "PersistType")
  364. {
  365. pi.SetValue(obj, EntityPersistType.Update, null);
  366. }
  367. }
  368. return obj;
  369. }
  370. /// <summary>
  371. /// 将DataTable转换为list(用的FOR赋值,效果一样,这种比较复杂)
  372. /// </summary>
  373. /// <typeparam name="T"></typeparam>
  374. /// <param name="dt"></param>
  375. /// <returns></returns>
  376. public static List<T> DataTable2List<T>(DataTable dt)
  377. {
  378. List<T> result = new List<T>();
  379. if (dt == null) return result;
  380. foreach (DataRow dr in dt.Rows)
  381. {
  382. T objj = (T)Activator.CreateInstance(typeof(T));
  383. PropertyInfo[] propertys = objj.GetType().GetProperties();
  384. foreach (PropertyInfo pi in propertys)
  385. {
  386. #region 属性与字段名称一致的进行赋值
  387. if (dt.Columns.Contains(pi.Name))
  388. {
  389. try
  390. {
  391. if (!Convert.IsDBNull(dr[pi.Name]))
  392. pi.SetValue(objj, dr[pi.Name], null);
  393. else
  394. pi.SetValue(objj, null, null);//为空,但不为Null
  395. }
  396. catch (Exception ex)
  397. {
  398. pi.SetValue(objj, null, null);//为空,但不为Null
  399. }
  400. }
  401. if (pi.Name == "PersistType")
  402. {
  403. pi.SetValue(objj, EntityPersistType.Update, null);
  404. }
  405. #endregion
  406. }
  407. result.Add(objj);
  408. }
  409. return result;
  410. }
  411. #endregion
  412. /// <summary>
  413. /// 设置初始值
  414. /// </summary>
  415. /// <param name="FieldName">字段名称</param>
  416. /// <param name="defaultValue">默认值</param>
  417. private static Dictionary<string, EntityValue> SetChanaged(string FieldName, object defaultValue, Dictionary<string, EntityValue> Values)
  418. {
  419. if (!Values.ContainsKey(FieldName))
  420. {
  421. EntityValue val = new EntityValue();
  422. val.OriginalValue = defaultValue;
  423. val.FieldName = FieldName;
  424. val.IsChanage = false;
  425. Values.Add(FieldName, val);
  426. }
  427. else
  428. {
  429. Values[FieldName].OriginalValue = defaultValue;
  430. Values[FieldName].IsChanage = false;
  431. }
  432. return Values;
  433. }
  434. }
  435. }