DataTableHelper.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Reflection;
  7. namespace Masuit.Tools.Core.Database
  8. {
  9. /// <summary>
  10. /// DataTable帮助类
  11. /// </summary>
  12. public static class DataTableHelper
  13. {
  14. /// <summary>
  15. /// 给DataTable增加一个自增列
  16. /// 如果DataTable 存在 identityid 字段 则 直接返回DataTable 不做任何处理
  17. /// </summary>
  18. /// <param name="dt">DataTable</param>
  19. /// <returns>返回Datatable 增加字段 identityid </returns>
  20. public static DataTable AddIdentityColumn(this DataTable dt)
  21. {
  22. if (!dt.Columns.Contains("identityid"))
  23. {
  24. dt.Columns.Add("identityid");
  25. for (int i = 0; i < dt.Rows.Count; i++)
  26. {
  27. dt.Rows[i]["identityid"] = (i + 1).ToString();
  28. }
  29. }
  30. return dt;
  31. }
  32. /// <summary>
  33. /// 检查DataTable 是否有数据行
  34. /// </summary>
  35. /// <param name="dt">DataTable</param>
  36. /// <returns>是否有数据行</returns>
  37. public static bool HasRows(this DataTable dt)
  38. {
  39. return dt.Rows.Count > 0;
  40. }
  41. /// <summary>
  42. /// datatable转List
  43. /// </summary>
  44. /// <typeparam name="T"></typeparam>
  45. /// <param name="dt"></param>
  46. /// <returns></returns>
  47. public static List<T> ToList<T>(this DataTable dt) where T : class, new()
  48. {
  49. List<T> list = new List<T>();
  50. using (dt)
  51. {
  52. if (dt == null || dt.Rows.Count == 0)
  53. {
  54. return list;
  55. }
  56. DataTableBuilder<T> eblist = DataTableBuilder<T>.CreateBuilder(dt.Rows[0]);
  57. foreach (DataRow info in dt.Rows)
  58. {
  59. list.Add(eblist.Build(info));
  60. }
  61. return list;
  62. }
  63. }
  64. /// <summary>
  65. /// 将泛型集合类转换成DataTable
  66. /// </summary>
  67. /// <typeparam name="T">集合项类型</typeparam>
  68. /// <param name="list">集合</param>
  69. /// <param name="tableName">表名</param>
  70. /// <returns>数据集(表)</returns>
  71. public static DataTable ToDataTable<T>(this IList<T> list, string tableName)
  72. {
  73. DataTable result = new DataTable();
  74. if (list.Count <= 0)
  75. {
  76. return result;
  77. }
  78. var properties = list[0].GetType().GetProperties();
  79. properties.ForEach(pi =>
  80. {
  81. var columnName = pi.GetCustomAttribute<DescriptionAttribute>()?.Description ?? pi.Name;
  82. result.Columns.Add(columnName, pi.PropertyType);
  83. });
  84. list.ForEach(item =>
  85. {
  86. ArrayList tempList = new ArrayList();
  87. foreach (PropertyInfo pi in properties)
  88. {
  89. object obj = pi.GetValue(item, null);
  90. tempList.Add(obj);
  91. }
  92. object[] array = tempList.ToArray();
  93. result.LoadDataRow(array, true);
  94. });
  95. result.TableName = tableName;
  96. return result;
  97. }
  98. /// <summary>
  99. /// 根据nameList里面的字段创建一个表格,返回该表格的DataTable
  100. /// </summary>
  101. /// <param name="nameList">包含字段信息的列表</param>
  102. /// <returns>DataTable</returns>
  103. public static DataTable CreateTable(this List<string> nameList)
  104. {
  105. if (nameList.Count <= 0)
  106. {
  107. return null;
  108. }
  109. var myDataTable = new DataTable();
  110. foreach (string columnName in nameList)
  111. {
  112. myDataTable.Columns.Add(columnName, typeof(string));
  113. }
  114. return myDataTable;
  115. }
  116. /// <summary>
  117. /// 通过字符列表创建表字段,字段格式可以是:<br/>
  118. /// 1) a,b,c,d,e<br/>
  119. /// 2) a|int,b|string,c|bool,d|decimal<br/>
  120. /// </summary>
  121. /// <param name="dt"></param>
  122. /// <param name="nameString">字符列表</param>
  123. /// <returns>内存表</returns>
  124. public static DataTable CreateTable(this DataTable dt, string nameString)
  125. {
  126. string[] nameArray = nameString.Split(',', ';');
  127. foreach (string item in nameArray)
  128. {
  129. if (!string.IsNullOrEmpty(item))
  130. {
  131. string[] subItems = item.Split('|');
  132. if (subItems.Length == 2)
  133. {
  134. dt.Columns.Add(subItems[0], ConvertType(subItems[1]));
  135. }
  136. else
  137. {
  138. dt.Columns.Add(subItems[0]);
  139. }
  140. }
  141. }
  142. return dt;
  143. }
  144. /// <summary>
  145. /// 根据类型名返回一个Type类型
  146. /// </summary>
  147. /// <param name="typeName">类型的名称</param>
  148. /// <returns>Type对象</returns>
  149. private static Type ConvertType(string typeName)
  150. {
  151. typeName = typeName.ToLower().Replace("system.", "");
  152. Type newType = typeof(string);
  153. switch (typeName)
  154. {
  155. case "boolean":
  156. case "bool":
  157. newType = typeof(bool);
  158. break;
  159. case "int16":
  160. case "short":
  161. newType = typeof(short);
  162. break;
  163. case "int32":
  164. case "int":
  165. newType = typeof(int);
  166. break;
  167. case "long":
  168. case "int64":
  169. newType = typeof(long);
  170. break;
  171. case "uint16":
  172. case "ushort":
  173. newType = typeof(ushort);
  174. break;
  175. case "uint32":
  176. case "uint":
  177. newType = typeof(uint);
  178. break;
  179. case "uint64":
  180. case "ulong":
  181. newType = typeof(ulong);
  182. break;
  183. case "single":
  184. case "float":
  185. newType = typeof(float);
  186. break;
  187. case "string":
  188. newType = typeof(string);
  189. break;
  190. case "guid":
  191. newType = typeof(Guid);
  192. break;
  193. case "decimal":
  194. newType = typeof(decimal);
  195. break;
  196. case "double":
  197. newType = typeof(double);
  198. break;
  199. case "datetime":
  200. newType = typeof(DateTime);
  201. break;
  202. case "byte":
  203. newType = typeof(byte);
  204. break;
  205. case "char":
  206. newType = typeof(char);
  207. break;
  208. }
  209. return newType;
  210. }
  211. /// <summary>
  212. /// 获得从DataRowCollection转换成的DataRow数组
  213. /// </summary>
  214. /// <param name="drc">DataRowCollection</param>
  215. /// <returns>DataRow数组</returns>
  216. public static DataRow[] GetDataRowArray(this DataRowCollection drc)
  217. {
  218. int count = drc.Count;
  219. var drs = new DataRow[count];
  220. for (int i = 0; i < count; i++)
  221. {
  222. drs[i] = drc[i];
  223. }
  224. return drs;
  225. }
  226. /// <summary>
  227. /// 将DataRow数组转换成DataTable,注意行数组的每个元素须具有相同的数据结构,
  228. /// 否则当有元素长度大于第一个元素时,抛出异常
  229. /// </summary>
  230. /// <param name="rows">行数组</param>
  231. /// <returns>将内存行组装成内存表</returns>
  232. public static DataTable GetTableFromRows(this DataRow[] rows)
  233. {
  234. if (rows.Length <= 0)
  235. {
  236. return new DataTable();
  237. }
  238. DataTable dt = rows[0].Table.Clone();
  239. dt.DefaultView.Sort = rows[0].Table.DefaultView.Sort;
  240. foreach (var t in rows)
  241. {
  242. dt.LoadDataRow(t.ItemArray, true);
  243. }
  244. return dt;
  245. }
  246. }
  247. }