DataToMod.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Ant.ORM;
  6. using System.Reflection;
  7. using System.Data;
  8. using Ant.Frame;
  9. namespace Ant.ORM
  10. {
  11. public static class DataToMod
  12. {
  13. #region 将DataTable转换成List列表
  14. /// <summary>
  15. /// 返回一条实体数据
  16. /// </summary>
  17. /// <typeparam name="T">实体</typeparam>
  18. /// <param name="dataTable">表数据</param>
  19. /// <returns></returns>
  20. public static object DataTableToEntity<T>(DataTable dataTable)
  21. {
  22. Dictionary<string, EntityValue> vl = new Dictionary<string, EntityValue>();
  23. T obj = Activator.CreateInstance<T>();
  24. if (dataTable.Rows.Count > 0)
  25. {
  26. DataRow row = dataTable.Rows[0];
  27. foreach (PropertyInfo pinfo in obj.GetType().GetProperties())
  28. {
  29. DataFieldColumnAttribute dfa = Attribute.GetCustomAttribute(pinfo, typeof(DataFieldColumnAttribute)) as DataFieldColumnAttribute;
  30. if (!object.Equals(dfa, null))
  31. {
  32. if (!Convert.IsDBNull(row[pinfo.Name]))
  33. {
  34. try
  35. {
  36. vl = SetChanaged(pinfo.Name, row[pinfo.Name], vl);
  37. pinfo.SetValue(obj, row[pinfo.Name], null);
  38. }
  39. catch (Exception ex)
  40. {
  41. pinfo.SetValue(obj, null, null);
  42. }
  43. }
  44. }
  45. if (pinfo.Name == "PersistType")
  46. {
  47. pinfo.SetValue(obj, EntityPersistType.Update, null);
  48. }
  49. if (pinfo.Name == "Values")
  50. {
  51. pinfo.SetValue(obj, vl, null);
  52. }
  53. }
  54. }
  55. return obj;
  56. }
  57. /// <summary>
  58. /// 返回一条实体数据
  59. /// </summary>
  60. /// <typeparam name="T">实体</typeparam>
  61. /// <param name="dataTable">表数据</param>
  62. /// <returns></returns>
  63. private static object DataTableToEntity<T>(DataSet ds)
  64. {
  65. Dictionary<string, EntityValue> vl = new Dictionary<string, EntityValue>();
  66. T obj = Activator.CreateInstance<T>();
  67. if (ds.Tables.Count > 0)
  68. {
  69. if (ds.Tables[0].Rows.Count > 0)
  70. {
  71. DataRow row = ds.Tables[0].Rows[0];
  72. foreach (PropertyInfo pinfo in obj.GetType().GetProperties())
  73. {
  74. DataFieldColumnAttribute dfa = Attribute.GetCustomAttribute(pinfo, typeof(DataFieldColumnAttribute)) as DataFieldColumnAttribute;
  75. if (!object.Equals(dfa, null))
  76. {
  77. if (!Convert.IsDBNull(row[pinfo.Name]))
  78. {
  79. try
  80. {
  81. vl = SetChanaged(pinfo.Name, row[pinfo.Name], vl);
  82. pinfo.SetValue(obj, row[pinfo.Name], null);
  83. }
  84. catch (Exception ex)
  85. {
  86. pinfo.SetValue(obj, null, null);
  87. }
  88. }
  89. }
  90. if (pinfo.Name == "PersistType")
  91. {
  92. pinfo.SetValue(obj, EntityPersistType.Update, null);
  93. }
  94. if (pinfo.Name == "Values")
  95. {
  96. pinfo.SetValue(obj, vl, null);
  97. }
  98. }
  99. }
  100. }
  101. return obj;
  102. }
  103. /// <summary>
  104. /// 返回实体集合
  105. /// </summary>
  106. /// <typeparam name="T">实体</typeparam>
  107. /// <param name="dataTable">表数据</param>
  108. /// <returns></returns>
  109. private static object DataTableToList<T>(DataTable dataTable)
  110. {
  111. List<T> list = new List<T>(); Dictionary<string, EntityValue> vl = new Dictionary<string, EntityValue>();
  112. try
  113. {
  114. foreach (DataRow row in dataTable.Rows)
  115. {
  116. T obj = Activator.CreateInstance<T>();
  117. foreach (PropertyInfo pinfo in obj.GetType().GetProperties())
  118. {
  119. DataFieldColumnAttribute dfa = Attribute.GetCustomAttribute(pinfo, typeof(DataFieldColumnAttribute)) as DataFieldColumnAttribute;
  120. if (!object.Equals(dfa, null))
  121. {
  122. try
  123. {
  124. if (!Convert.IsDBNull(row[pinfo.Name]))
  125. {
  126. vl = SetChanaged(pinfo.Name, row[pinfo.Name], vl);
  127. pinfo.SetValue(obj, row[pinfo.Name], null);
  128. }
  129. else
  130. pinfo.SetValue(obj, null, null);
  131. }
  132. catch (Exception ex)
  133. {
  134. pinfo.SetValue(obj, null, null);
  135. }
  136. }
  137. if (pinfo.Name.Contains("PersistType"))
  138. {
  139. pinfo.SetValue(obj, EntityPersistType.Update, null);
  140. }
  141. if (pinfo.Name.Contains("Values"))
  142. {
  143. pinfo.SetValue(obj, vl, null);
  144. }
  145. }
  146. list.Add(obj);
  147. }
  148. }
  149. catch (Exception ex)
  150. {
  151. }
  152. return list;
  153. }
  154. /// <summary>
  155. /// 将表数据转换成实体
  156. /// </summary>
  157. /// <param name="dataTable">表数据</param>
  158. /// <param name="enty">实体</param>
  159. /// <returns></returns>
  160. private static object DataTableToList(DataTable dt, object enty)
  161. {
  162. List<object> list = new List<object>();
  163. if (dt == null) return list;
  164. object objj = Activator.CreateInstance(enty.GetType());
  165. foreach (DataRow row in dt.Rows)
  166. {
  167. foreach (PropertyInfo pinfo in enty.GetType().GetProperties())
  168. {
  169. DataFieldColumnAttribute dfa = Attribute.GetCustomAttribute(pinfo, typeof(DataFieldColumnAttribute)) as DataFieldColumnAttribute;
  170. if (!object.Equals(dfa, null))
  171. {
  172. try
  173. {
  174. if (!Convert.IsDBNull(row[pinfo.Name]))
  175. {
  176. pinfo.SetValue(objj, row[pinfo.Name], null);
  177. }
  178. else
  179. {
  180. pinfo.SetValue(objj, null, null);
  181. }
  182. }
  183. catch (Exception ex)
  184. {
  185. pinfo.SetValue(objj, null, null);
  186. }
  187. }
  188. if (pinfo.Name.Contains("PersistType"))
  189. {
  190. pinfo.SetValue(objj, EntityPersistType.Update, null);
  191. }
  192. }
  193. list.Add(objj);
  194. }
  195. return list;
  196. }
  197. /// <summary>
  198. /// 这样也可以不过entity每次的值还在,缺点就是无法清除原先的值
  199. /// </summary>
  200. /// <param name="dr"></param>
  201. /// <param name="md"></param>
  202. /// <param name="enty"></param>
  203. /// <returns></returns>
  204. public static object DataTable2List(IDataReader dr, object enty)
  205. {
  206. object objj = Activator.CreateInstance(enty.GetType());
  207. if (dr == null) return objj;
  208. PropertyInfo[] propertys = enty.GetType().GetProperties();
  209. foreach (PropertyInfo pi in propertys)
  210. {
  211. DataTable MyDataTable = dr.GetSchemaTable();
  212. foreach (DataRow row in MyDataTable.Rows)
  213. {
  214. foreach (DataColumn col in MyDataTable.Columns)
  215. {
  216. #region 属性与字段名称一致的进行赋值
  217. if (pi.Name.ToLower().Equals(row[0].ToString().ToLower()))
  218. {
  219. try
  220. {
  221. if (!Convert.IsDBNull(dr[row[0].ToString()]))
  222. {
  223. pi.SetValue(objj, dr[pi.Name], null);
  224. }
  225. else
  226. pi.SetValue(objj, null, null);//为空,但不为Null
  227. }
  228. catch (Exception ex)
  229. {
  230. pi.SetValue(objj, null, null);//为空,但不为Null
  231. }
  232. }
  233. #endregion
  234. }
  235. if (pi.Name.Contains("PersistType"))
  236. {
  237. pi.SetValue(objj, EntityPersistType.Update, null);
  238. }
  239. }
  240. }
  241. return objj;
  242. }
  243. /// <summary>
  244. /// 获取一条实体
  245. /// </summary>
  246. /// <param name="dt"></param>
  247. /// <param name="md"></param>
  248. /// <param name="enty"></param>
  249. /// <returns></returns>
  250. public static object DataTableToEntity(DataTable dt, object enty)
  251. {
  252. object objj = Activator.CreateInstance(enty.GetType());
  253. if (dt == null) return objj;
  254. if (dt.Rows.Count == 0) return objj;
  255. PropertyInfo[] propertys = enty.GetType().GetProperties();
  256. foreach (DataRow dr in dt.Rows)
  257. {
  258. foreach (PropertyInfo pi in propertys)
  259. {
  260. #region 属性与字段名称一致的进行赋值
  261. if (dt.Columns.Contains(pi.Name))
  262. {
  263. try
  264. {
  265. //if (!Convert.IsDBNull(dr[pi.Name]))
  266. if ((Object.Equals(dr[pi.Name], null)) || (Object.Equals(dr[pi.Name], DBNull.Value)))
  267. pi.SetValue(objj, null, null);//为空,但不为Null
  268. else
  269. pi.SetValue(objj, dr[pi.Name], null);
  270. }
  271. catch (Exception ex)
  272. {
  273. pi.SetValue(objj, null, null);//为空,但不为Null
  274. }
  275. }
  276. #endregion
  277. if (pi.Name.Contains("PersistType"))
  278. {
  279. pi.SetValue(objj, EntityPersistType.Update, null);
  280. }
  281. }
  282. }
  283. return objj;
  284. }
  285. /// <summary>
  286. /// 这样也可以不过entity每次的值还在,缺点就是无法清除原先的值
  287. /// </summary>
  288. /// <param name="dr"></param>
  289. /// <param name="md"></param>
  290. /// <param name="enty"></param>
  291. /// <returns></returns>
  292. public static object DataTable2List(DataTable dt, object enty)
  293. {
  294. List<object> list = new List<object>();
  295. if (dt == null) return list;
  296. PropertyInfo[] propertys = enty.GetType().GetProperties();
  297. foreach (DataRow dr in dt.Rows)
  298. {
  299. object objj = Activator.CreateInstance(enty.GetType());
  300. foreach (PropertyInfo pi in propertys)
  301. {
  302. #region 属性与字段名称一致的进行赋值
  303. if (dt.Columns.Contains(pi.Name))
  304. {
  305. try
  306. {
  307. //if (!Convert.IsDBNull(dr[pi.Name]))
  308. if ((Object.Equals(dr[pi.Name], null)) || (Object.Equals(dr[pi.Name], DBNull.Value)))
  309. pi.SetValue(objj, null, null);//为空,但不为Null
  310. else
  311. pi.SetValue(objj, dt.Rows[0][pi.Name], null);
  312. }
  313. catch (Exception ex)
  314. {
  315. pi.SetValue(objj, null, null);//为空,但不为Null
  316. }
  317. }
  318. #endregion
  319. if (pi.Name.Contains("PersistType"))
  320. {
  321. pi.SetValue(objj, EntityPersistType.Update, null);
  322. }
  323. }
  324. list.Add(objj);
  325. }
  326. return list;
  327. }
  328. /// <summary>
  329. /// 将DataTable转换为list(用的FOR赋值,效果一样,这种比较复杂)
  330. /// </summary>
  331. /// <typeparam name="T"></typeparam>
  332. /// <param name="dt"></param>
  333. /// <returns></returns>
  334. public static T DataTable2List<T>(IDataReader dr)
  335. {
  336. T obj = (T)Activator.CreateInstance(typeof(T));
  337. if (dr == null) return obj;
  338. PropertyInfo[] propertys = obj.GetType().GetProperties();
  339. foreach (PropertyInfo pi in propertys)
  340. {
  341. DataTable MyDataTable = dr.GetSchemaTable();
  342. foreach (DataRow row in MyDataTable.Rows)
  343. {
  344. foreach (DataColumn col in MyDataTable.Columns)
  345. {
  346. #region 属性与字段名称一致的进行赋值
  347. if (pi.Name.ToLower().Equals(row[0].ToString().ToLower()))
  348. {
  349. try
  350. {
  351. if (!Convert.IsDBNull(dr[row[0].ToString()]))
  352. {
  353. pi.SetValue(obj, dr[pi.Name], null);
  354. }
  355. else
  356. pi.SetValue(obj, null, null);//为空,但不为Null
  357. }
  358. catch (Exception ex)
  359. {
  360. pi.SetValue(obj, null, null);//为空,但不为Null
  361. }
  362. }
  363. #endregion
  364. }
  365. }
  366. if (pi.Name.Contains("PersistType"))
  367. {
  368. pi.SetValue(obj, EntityPersistType.Update, null);
  369. }
  370. }
  371. return obj;
  372. }
  373. /// <summary>
  374. /// 将DataTable转换为list(用的FOR赋值,效果一样,这种比较复杂)
  375. /// </summary>
  376. /// <typeparam name="T"></typeparam>
  377. /// <param name="dt"></param>
  378. /// <returns></returns>
  379. public static List<T> DataTable2List<T>(DataTable dt)
  380. {
  381. List<T> result = new List<T>();
  382. if (dt == null) return result;
  383. foreach (DataRow dr in dt.Rows)
  384. {
  385. T objj = (T)Activator.CreateInstance(typeof(T));
  386. //PropertyInfo currentInfo = objj.GetType().GetProperty("列名");
  387. PropertyInfo[] propertys = objj.GetType().GetProperties();
  388. foreach (PropertyInfo pi in propertys)
  389. {
  390. #region 属性与字段名称一致的进行赋值
  391. if (dt.Columns.Contains(pi.Name))
  392. {
  393. try
  394. {
  395. //if (!Convert.IsDBNull(dr[pi.Name]))
  396. if ((Object.Equals(dr[pi.Name], null)) || (Object.Equals(dr[pi.Name], DBNull.Value)))
  397. pi.SetValue(objj, null, null);//为空,但不为Null
  398. else
  399. pi.SetValue(objj, dr[pi.Name], null);
  400. }
  401. catch (Exception ex)
  402. {
  403. pi.SetValue(objj, null, null);//为空,但不为Null
  404. }
  405. }
  406. #endregion
  407. if (pi.Name.Contains("PersistType"))
  408. {
  409. pi.SetValue(objj, EntityPersistType.Update, null);
  410. }
  411. }
  412. result.Add(objj);
  413. }
  414. return result;
  415. }
  416. #endregion
  417. #region 备份
  418. ///// <summary>
  419. ///// 将DataTable转换为list(用的FOR赋值,效果一样,这种比较复杂)
  420. ///// </summary>
  421. ///// <typeparam name="T"></typeparam>
  422. ///// <param name="dt"></param>
  423. ///// <returns></returns>
  424. //public static List<T> DataTable2List<T>(DataTable dt)
  425. //{
  426. // List<T> result = new List<T>(); string colname = "";
  427. // MetaData md = MetaDataManager.GetMetaData(typeof(T));
  428. // if (dt == null) return result;
  429. // for (int j = 0; j < dt.Rows.Count; j++)
  430. // {
  431. // T objj = (T)Activator.CreateInstance(typeof(T));
  432. // PropertyInfo[] propertys = objj.GetType().GetProperties();
  433. // foreach (PropertyInfo pi in propertys)
  434. // {
  435. // colname = "";
  436. // for (int i = 0; i < dt.Columns.Count; i++)
  437. // {
  438. // if (pi.Name.ToLower().Equals(dt.Columns[i].ColumnName.ToLower()))
  439. // {
  440. // colname = pi.Name; break;
  441. // }
  442. // }
  443. // if (!string.IsNullOrEmpty(colname))
  444. // {
  445. // #region 属性与字段名称一致的进行赋值
  446. // //if (dt.Rows[j][colname] != DBNull.Value && dt.Rows[j][colname] != null && !object.Equals(dt.Rows[j][colname], null))
  447. // if (!Convert.IsDBNull(dt.Rows[j][colname]))
  448. // {
  449. // try
  450. // {
  451. // pi.SetValue(objj, dt.Rows[j][md.FieldMeteDatas[colname].ColumnName], null);
  452. // }
  453. // catch (Exception ex)
  454. // {
  455. // pi.SetValue(objj, null, null);//为空,但不为Null
  456. //
  457. // }
  458. // }
  459. // else
  460. // {
  461. // pi.SetValue(objj, null, null);//为空,但不为Null
  462. // }
  463. // #endregion
  464. // }
  465. // if (pi.Name == "PersistType")
  466. // {
  467. // pi.SetValue(objj, EntityPersistType.Update, null);
  468. // }
  469. // }
  470. // result.Add(objj);
  471. // }
  472. // return result;
  473. //}
  474. /// <summary>
  475. /// 暂时不用
  476. /// </summary>
  477. /// <typeparam name="T"></typeparam>
  478. /// <param name="dr"></param>
  479. /// <returns></returns>
  480. //public static object DataTable2List<T>(IDataReader dr)
  481. //{
  482. // T obj = (T)Activator.CreateInstance(typeof(T));
  483. // MetaData md = MetaDataManager.GetMetaData(typeof(T));
  484. // if (dr == null) return obj;
  485. // PropertyInfo[] propertys = obj.GetType().GetProperties();
  486. // foreach (PropertyInfo pi in propertys)
  487. // {
  488. // DataTable MyDataTable = dr.GetSchemaTable();
  489. // foreach (DataRow row in MyDataTable.Rows)
  490. // {
  491. // foreach (DataColumn col in MyDataTable.Columns)
  492. // {
  493. // #region 属性与字段名称一致的进行赋值
  494. // if (pi.Name.ToLower().Equals(row[0].ToString().ToLower()))
  495. // {
  496. // //if (dr[row[0].ToString()] != DBNull.Value)
  497. // //if (dr[row[0].ToString()] != DBNull.Value && dr[row[0].ToString()] != null && !object.Equals(dr[row[0].ToString()], null))
  498. // if (!Convert.IsDBNull(dr[row[0].ToString()]))
  499. // {
  500. // if (pi.PropertyType.ToString().ToLower().Contains("system.int64"))
  501. // {
  502. // pi.SetValue(obj, Int64.Parse(dr[row[0].ToString()].ToString()), null);
  503. // }
  504. // if (pi.PropertyType.ToString().ToLower().Contains("system.int32"))
  505. // {
  506. // pi.SetValue(obj, Int32.Parse(dr[row[0].ToString()].ToString()), null);
  507. // }
  508. // else if (pi.PropertyType.ToString().ToLower().Contains("system.datetime"))
  509. // {
  510. // pi.SetValue(obj, Convert.ToDateTime(dr[row[0].ToString()].ToString()), null);
  511. // }
  512. // else if (pi.PropertyType.ToString().ToLower().Contains("system.boolean"))
  513. // {
  514. // pi.SetValue(obj, Convert.ToBoolean(dr[row[0].ToString()].ToString()), null);
  515. // }
  516. // else if (pi.PropertyType.ToString().ToLower().Contains("system.single"))
  517. // {
  518. // pi.SetValue(obj, Convert.ToSingle(dr[row[0].ToString()].ToString()), null);
  519. // }
  520. // else if (pi.PropertyType.ToString().ToLower().Contains("system.double"))
  521. // {
  522. // pi.SetValue(obj, Convert.ToDouble(dr[row[0].ToString()].ToString()), null);
  523. // }
  524. // else
  525. // {
  526. // pi.SetValue(obj, dr[md.FieldMeteDatas[pi.Name].ColumnName], null);
  527. // }
  528. // }
  529. // else
  530. // pi.SetValue(obj, null, null);//为空,但不为Null
  531. // }
  532. // #endregion
  533. // }
  534. // }
  535. // if (pi.Name == "PersistType")
  536. // {
  537. // pi.SetValue(obj, EntityPersistType.Update, null);
  538. // }
  539. // }
  540. // return obj;
  541. //}
  542. #endregion
  543. /// <summary>
  544. /// 设置初始值
  545. /// </summary>
  546. /// <param name="FieldName">字段名称</param>
  547. /// <param name="defaultValue">默认值</param>
  548. private static Dictionary<string, EntityValue> SetChanaged(string FieldName, object defaultValue, Dictionary<string, EntityValue> Values)
  549. {
  550. if (!Values.ContainsKey(FieldName))
  551. {
  552. EntityValue val = new EntityValue();
  553. val.OriginalValue = defaultValue;
  554. val.FieldName = FieldName;
  555. val.IsChanage = false;
  556. Values.Add(FieldName, val);
  557. }
  558. else
  559. {
  560. Values[FieldName].OriginalValue = defaultValue;
  561. Values[FieldName].IsChanage = false;
  562. }
  563. return Values;
  564. }
  565. }
  566. }