DataSetUtil.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Reflection;
  3. using System.Collections;
  4. using System.Resources;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Data.OleDb;
  8. using System.Collections.Generic;
  9. namespace Core.Mes.Client.Common.Util
  10. {
  11. public class DataSetUtil
  12. {
  13. public static int Count(DataSet ds)
  14. {
  15. try
  16. {
  17. return ds.Tables[0].Rows.Count;
  18. }
  19. catch
  20. {
  21. }
  22. return 0;
  23. }
  24. public static DataSet GetDataSetWithTbColumnAndSourceDataSet(DataSet dsSource, string[] columnList)
  25. {
  26. if (Util.DataSetUtil.Count(dsSource) == 0)
  27. return dsSource;
  28. if (columnList.Length == 0)
  29. return dsSource;
  30. DataSet dsNew = new DataSet();
  31. DataTable dtNew = new DataTable();
  32. DataColumn dc = null;
  33. dsNew.Tables.Add(dtNew);
  34. foreach (string s in columnList)
  35. {
  36. if (dsSource.Tables[0].Columns.Contains(s.ToUpper()))
  37. dc = new DataColumn(s.ToUpper(), dsSource.Tables[0].Columns[s].DataType);
  38. else
  39. dc = new DataColumn(s.ToUpper(), System.Type.GetType("System.String"));
  40. dsNew.Tables[0].Columns.Add(dc);
  41. }
  42. dsNew.Tables[0].Merge(dsSource.Tables[0], false, System.Data.MissingSchemaAction.Ignore);
  43. dsNew.AcceptChanges();
  44. return dsNew;
  45. }
  46. /// <summary>
  47. /// 合并两个DataSet
  48. /// </summary>
  49. /// <param name="dsSouce"></param>
  50. /// <param name="dsNewData"></param>
  51. /// <param name="values"></param>
  52. /// <param name="columnName"></param>
  53. /// <returns></returns>
  54. public static DataSet GetMergeDataSetByAddNewData(DataSet dsSouce, DataSet dsNewData, List<string> values, string columnName)
  55. {
  56. DataSet ds = dsSouce.Copy();
  57. if (Count(ds) == 0)
  58. return ds;
  59. if (Count(dsNewData) == 0)
  60. return ds;
  61. foreach (DataRow dr in ds.Tables[0].Rows)
  62. {
  63. DataRow[] drs = dsNewData.Tables[0].Select(string.Format("{0} = '{1}' ", columnName, dr[columnName].ToString()));
  64. if (drs == null || drs.Length == 0)
  65. continue;
  66. try
  67. {
  68. foreach (string s in values)
  69. {
  70. try
  71. {
  72. dr[s] = drs[0][s];
  73. }
  74. catch
  75. {
  76. }
  77. }
  78. }
  79. catch
  80. {
  81. }
  82. }
  83. return ds;
  84. }
  85. }
  86. }