DataTableTool.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using Azylee.Core.DataUtils.StringUtils;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Linq;
  7. using System.Text;
  8. namespace Azylee.Core.DataUtils.DataTableUtils
  9. {
  10. public class DataTableTool
  11. {
  12. public static bool Ok(DataTable table)
  13. {
  14. if (table != null && table.Rows != null && table.Rows.Count > 0)
  15. {
  16. return true;
  17. }
  18. return false;
  19. }
  20. public static List<Dictionary<string, object>> ToDictionary(DataTable data, NameType nameType = NameType.NONE)
  21. {
  22. List<Dictionary<string, object>> result = new List<Dictionary<string, object>>();
  23. if (Ok(data))
  24. {
  25. List<string> colNames = new List<string>();
  26. foreach (DataColumn col in data.Columns) colNames.Add(col.ColumnName);
  27. foreach (DataRow row in data.Rows)
  28. {
  29. Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
  30. foreach (string name in colNames)
  31. {
  32. keyValuePairs.Add(NameFormat.Format(name, nameType), row[name]);
  33. }
  34. result.Add(keyValuePairs);
  35. }
  36. }
  37. return result;
  38. }
  39. }
  40. }