DataTableModelHelp.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. namespace Ant.ORM
  8. {
  9. /// <summary>
  10. /// DataTable与实体类互相转换
  11. /// </summary>
  12. /// <typeparam name="T">实体类</typeparam>
  13. public class DataTableModelHelp<T> where T : new()
  14. {
  15. #region DataTable转换成实体类
  16. /// <summary>
  17. /// 填充对象列表:用DataSet的第一个表填充分体类
  18. /// </summary>
  19. /// <param name="ds">DataSet</param>
  20. /// <returns></returns>
  21. public List<T> FillModel(DataSet ds)
  22. {
  23. if (ds == null || ds.Tables[0] == null || ds.Tables[0].Rows.Count == 0)
  24. {
  25. return null;
  26. }
  27. else
  28. {
  29. return FillModel(ds.Tables[0]);
  30. }
  31. }
  32. /// <summary>
  33. /// 填充对象列表:用DataSet的第index个表填充分体类
  34. /// </summary>
  35. public List<T> FillModel(DataSet ds, int index)
  36. {
  37. if (ds == null || ds.Tables.Count <= index || ds.Tables[index].Rows.Count == 0)
  38. {
  39. return null;
  40. }
  41. else
  42. {
  43. return FillModel(ds.Tables[index]);
  44. }
  45. }
  46. /// <summary>
  47. /// 填充对象列表:用DataTable填充分体类
  48. /// </summary>
  49. public List<T> FillModel(DataTable dt)
  50. {
  51. if (dt == null || dt.Rows.Count == 0)
  52. {
  53. return null;
  54. }
  55. List<T> modelList = new List<T>();
  56. foreach (DataRow dr in dt.Rows)
  57. {
  58. //T model = (T)Activator.CreateInstance(typeof(T));
  59. T model = new T();
  60. foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
  61. {
  62. model.GetType().GetProperty(propertyInfo.Name).SetValue(model, dr[propertyInfo.Name], null);
  63. }
  64. modelList.Add(model);
  65. }
  66. return modelList;
  67. }
  68. /// <summary>
  69. /// 填充对象:用DataRow填充分体类
  70. /// </summary>
  71. public T FillModel(DataRow dr)
  72. {
  73. if (dr == null)
  74. {
  75. return default(T);
  76. }
  77. //T model = (T)Activator.CreateInstance(typeof(T));
  78. T model = new T();
  79. foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
  80. {
  81. model.GetType().GetProperty(propertyInfo.Name).SetValue(model, dr[propertyInfo.Name], null);
  82. }
  83. return model;
  84. }
  85. #endregion
  86. #region 实体类转换成DataTable
  87. /// <summary>
  88. /// 实体类转换成DataSet
  89. /// </summary>
  90. /// <param name="modelList">实体类列表</param>
  91. /// <returns></returns>
  92. public DataSet FillDataSet(List<T> modelList)
  93. {
  94. if (modelList == null || modelList.Count == 0)
  95. {
  96. return null;
  97. }
  98. else
  99. {
  100. DataSet ds = new DataSet();
  101. ds.Tables.Add(FillDataTable(modelList));
  102. return ds;
  103. }
  104. }
  105. /// <summary>
  106. /// 实体类转换成DataTable
  107. /// </summary>
  108. /// <param name="modelList">实体类列表</param>
  109. /// <returns></returns>
  110. public DataTable FillDataTable(List<T> modelList)
  111. {
  112. if (modelList == null || modelList.Count == 0)
  113. {
  114. return null;
  115. }
  116. DataTable dt = CreateData(modelList[0]);
  117. foreach (T model in modelList)
  118. {
  119. DataRow dataRow = dt.NewRow();
  120. foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
  121. {
  122. dataRow[propertyInfo.Name] = propertyInfo.GetValue(model, null);
  123. }
  124. dt.Rows.Add(dataRow);
  125. }
  126. return dt;
  127. }
  128. /// <summary>
  129. /// 按照实体类获得表布局
  130. /// </summary>
  131. /// <param name="model">实体类</param>
  132. /// <returns></returns>
  133. private DataTable CreateData(T model)
  134. {
  135. DataTable dataTable = new DataTable(typeof(T).Name);
  136. foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
  137. {
  138. dataTable.Columns.Add(new DataColumn(propertyInfo.Name, propertyInfo.PropertyType));
  139. }
  140. return dataTable;
  141. }
  142. #endregion
  143. }
  144. }