DataTableHelper.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Reflection;
  6. namespace Masuit.Tools.Core.Database
  7. {
  8. /// <summary>
  9. /// DataTable帮助类
  10. /// </summary>
  11. public static class DataTableHelper
  12. {
  13. /// <summary>
  14. /// 给DataTable增加一个自增列
  15. /// 如果DataTable 存在 identityid 字段 则 直接返回DataTable 不做任何处理
  16. /// </summary>
  17. /// <param name="dt">DataTable</param>
  18. /// <returns>返回Datatable 增加字段 identityid </returns>
  19. /// <exception cref="DuplicateNameException">The collection already has a column with the specified name. (The comparison is not case-sensitive.) </exception>
  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. /// <returns>数据集(表)</returns>
  70. public static DataTable ToDataTable<T>(this IList<T> list)
  71. {
  72. return ToDataTable(list, null);
  73. }
  74. /// <summary>
  75. /// 将泛型集合类转换成DataTable
  76. /// </summary>
  77. /// <typeparam name="T">集合项类型</typeparam>
  78. /// <param name="list">集合</param>
  79. /// <param name="propertyName">需要返回的列的列名</param>
  80. /// <returns>数据集(表)</returns>
  81. public static DataTable ToDataTable<T>(this IList<T> list, params string[] propertyName)
  82. {
  83. List<string> propertyNameList = new List<string>();
  84. if (propertyName != null)
  85. {
  86. propertyNameList.AddRange(propertyName);
  87. }
  88. DataTable result = new DataTable();
  89. if (list.Count <= 0)
  90. {
  91. return result;
  92. }
  93. PropertyInfo[] propertys = list[0].GetType().GetProperties();
  94. propertys.ForEach(pi =>
  95. {
  96. if (propertyNameList.Count == 0)
  97. {
  98. result.Columns.Add(pi.Name, pi.PropertyType);
  99. }
  100. else
  101. {
  102. if (propertyNameList.Contains(pi.Name))
  103. {
  104. result.Columns.Add(pi.Name, pi.PropertyType);
  105. }
  106. }
  107. });
  108. list.ForEach(item =>
  109. {
  110. ArrayList tempList = new ArrayList();
  111. foreach (PropertyInfo pi in propertys)
  112. {
  113. if (propertyNameList.Count == 0)
  114. {
  115. object obj = pi.GetValue(item, null);
  116. tempList.Add(obj);
  117. }
  118. else
  119. {
  120. if (propertyNameList.Contains(pi.Name))
  121. {
  122. object obj = pi.GetValue(item, null);
  123. tempList.Add(obj);
  124. }
  125. }
  126. }
  127. object[] array = tempList.ToArray();
  128. result.LoadDataRow(array, true);
  129. });
  130. return result;
  131. }
  132. /// <summary>
  133. /// 根据nameList里面的字段创建一个表格,返回该表格的DataTable
  134. /// </summary>
  135. /// <param name="nameList">包含字段信息的列表</param>
  136. /// <returns>DataTable</returns>
  137. public static DataTable CreateTable(this List<string> nameList)
  138. {
  139. if (nameList.Count <= 0)
  140. {
  141. return null;
  142. }
  143. var myDataTable = new DataTable();
  144. foreach (string columnName in nameList)
  145. {
  146. myDataTable.Columns.Add(columnName, typeof(string));
  147. }
  148. return myDataTable;
  149. }
  150. /// <summary>
  151. /// 通过字符列表创建表字段,字段格式可以是:<br/>
  152. /// 1) a,b,c,d,e<br/>
  153. /// 2) a|int,b|string,c|bool,d|decimal<br/>
  154. /// </summary>
  155. /// <param name="dt"></param>
  156. /// <param name="nameString">字符列表</param>
  157. /// <returns>内存表</returns>
  158. public static DataTable CreateTable(this DataTable dt, string nameString)
  159. {
  160. string[] nameArray = nameString.Split(',', ';');
  161. foreach (string item in nameArray)
  162. {
  163. if (!string.IsNullOrEmpty(item))
  164. {
  165. string[] subItems = item.Split('|');
  166. if (subItems.Length == 2)
  167. {
  168. dt.Columns.Add(subItems[0], ConvertType(subItems[1]));
  169. }
  170. else
  171. {
  172. dt.Columns.Add(subItems[0]);
  173. }
  174. }
  175. }
  176. return dt;
  177. }
  178. /// <summary>
  179. /// 根据类型名返回一个Type类型
  180. /// </summary>
  181. /// <param name="typeName">类型的名称</param>
  182. /// <returns>Type对象</returns>
  183. private static Type ConvertType(string typeName)
  184. {
  185. typeName = typeName.ToLower().Replace("system.", "");
  186. Type newType = typeof(string);
  187. switch (typeName)
  188. {
  189. case "boolean":
  190. case "bool":
  191. newType = typeof(bool);
  192. break;
  193. case "int16":
  194. case "short":
  195. newType = typeof(short);
  196. break;
  197. case "int32":
  198. case "int":
  199. newType = typeof(int);
  200. break;
  201. case "long":
  202. case "int64":
  203. newType = typeof(long);
  204. break;
  205. case "uint16":
  206. case "ushort":
  207. newType = typeof(ushort);
  208. break;
  209. case "uint32":
  210. case "uint":
  211. newType = typeof(uint);
  212. break;
  213. case "uint64":
  214. case "ulong":
  215. newType = typeof(ulong);
  216. break;
  217. case "single":
  218. case "float":
  219. newType = typeof(float);
  220. break;
  221. case "string":
  222. newType = typeof(string);
  223. break;
  224. case "guid":
  225. newType = typeof(Guid);
  226. break;
  227. case "decimal":
  228. newType = typeof(decimal);
  229. break;
  230. case "double":
  231. newType = typeof(double);
  232. break;
  233. case "datetime":
  234. newType = typeof(DateTime);
  235. break;
  236. case "byte":
  237. newType = typeof(byte);
  238. break;
  239. case "char":
  240. newType = typeof(char);
  241. break;
  242. }
  243. return newType;
  244. }
  245. /// <summary>
  246. /// 获得从DataRowCollection转换成的DataRow数组
  247. /// </summary>
  248. /// <param name="drc">DataRowCollection</param>
  249. /// <returns>DataRow数组</returns>
  250. public static DataRow[] GetDataRowArray(this DataRowCollection drc)
  251. {
  252. int count = drc.Count;
  253. DataRow[] drs = new DataRow[count];
  254. for (int i = 0; i < count; i++)
  255. {
  256. drs[i] = drc[i];
  257. }
  258. return drs;
  259. }
  260. /// <summary>
  261. /// 将DataRow数组转换成DataTable,注意行数组的每个元素须具有相同的数据结构,
  262. /// 否则当有元素长度大于第一个元素时,抛出异常
  263. /// </summary>
  264. /// <param name="rows">行数组</param>
  265. /// <returns>将内存行组装成内存表</returns>
  266. public static DataTable GetTableFromRows(this DataRow[] rows)
  267. {
  268. if (rows.Length <= 0)
  269. {
  270. return new DataTable();
  271. }
  272. DataTable dt = rows[0].Table.Clone();
  273. dt.DefaultView.Sort = rows[0].Table.DefaultView.Sort;
  274. foreach (var t in rows)
  275. {
  276. dt.LoadDataRow(t.ItemArray, true);
  277. }
  278. return dt;
  279. }
  280. }
  281. }