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