DataSetUtil.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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.Collections.Generic;
  8. namespace Core.StlMes.Client.Sale.SaleFundMgt.Util
  9. {
  10. public class DataSetUtil
  11. {
  12. #region 获取一个ds的行数,为null or table==0 or rows = 0 都返回0
  13. /// <summary>
  14. /// 获取一个ds的行数
  15. /// </summary>
  16. /// <param name="ds"></param>
  17. /// <returns></returns>
  18. public static int Count(DataSet ds)
  19. {
  20. try
  21. {
  22. return ds.Tables[0].Rows.Count;
  23. }
  24. catch
  25. {
  26. }
  27. return 0;
  28. }
  29. #endregion
  30. #region 获取整个系统中出现的为number类型的字段,并返回hstb
  31. public static Hashtable GetNumberColumnByHsTb()
  32. {
  33. string[] columnName = new string[]
  34. {
  35. "QUANTITY","WEIGHT","PRICE","MONEY","BASEPRICE",
  36. "ALLWEIGHT","TRANSMONEY","INVOICETRANSMONEY","BPRICE","PLY",
  37. "WIDTH","LENGTH","ZYFMONEY","TESTPRICE","TRANSMONEYQC",
  38. "TRANSMONEYSY1","CARFMONEY","FCPRICE"
  39. };
  40. Hashtable hs = new Hashtable();
  41. foreach (string s in columnName)
  42. {
  43. try
  44. {
  45. hs.Add(s, "System.Decimal");
  46. }
  47. catch
  48. {
  49. }
  50. }
  51. return hs;
  52. }
  53. #endregion
  54. #region 根据传入的字段组合一个ds,并返回,ds 字段默认类型string
  55. /// <summary>
  56. /// 根据传入的字段组合一个ds,并返回,ds 字段默认类型string
  57. /// </summary>
  58. /// <param name="columnName"></param>
  59. /// <returns></returns>
  60. public static DataSet GetDataSetByTbColumn(string[] columnName)
  61. {
  62. if (columnName == null || columnName.Length == 0)
  63. return null;
  64. DataSet ds = new DataSet();
  65. DataTable dt = new DataTable();
  66. DataColumn column = null;
  67. foreach (string s in columnName)
  68. {
  69. column = new DataColumn(s, System.Type.GetType("System.String"));
  70. dt.Columns.Add(s);
  71. }
  72. ds.Tables.Add(dt);
  73. return ds;
  74. }
  75. #endregion
  76. #region 根据字段名称以及字段类型组合ds,没有指定字段类型的默认string
  77. /// <summary>
  78. /// 根据字段名称以及字段类型组合ds,没有指定字段类型的默认string
  79. /// </summary>
  80. /// <param name="columnName"></param>
  81. /// <param name="hs"></param>
  82. /// <returns></returns>
  83. public static DataSet GetDataSetByTbColumn(string[] columnName, Hashtable hs)
  84. {
  85. if (columnName == null || columnName.Length == 0 || hs == null)
  86. return null;
  87. DataSet ds = new DataSet();
  88. DataTable dt = new DataTable();
  89. DataColumn column = null;
  90. foreach (string s in columnName)
  91. {
  92. try
  93. {
  94. if (hs.Contains(s))
  95. {
  96. column = new DataColumn(s, System.Type.GetType(hs[s].ToString()));
  97. }
  98. else
  99. {
  100. column = new DataColumn(s, System.Type.GetType("System.String"));
  101. }
  102. dt.Columns.Add(s);
  103. }
  104. catch
  105. {
  106. }
  107. }
  108. ds.Tables.Add(dt);
  109. return ds;
  110. }
  111. /// <summary>
  112. /// 按给定的字段列表,columnList,组合一个ds,名称和dssource相同,类型从dssource查找
  113. /// 并填充
  114. /// </summary>
  115. /// <param name="dsSource"></param>
  116. /// <param name="columnList"></param>
  117. /// <returns></returns>
  118. public static DataSet GetDataSetWithTbColumnAndSourceDataSet(DataSet dsSource,string [] columnList)
  119. {
  120. if (Util.DataSetUtil.Count(dsSource) == 0)
  121. return dsSource;
  122. if (columnList.Length == 0)
  123. return dsSource;
  124. DataSet dsNew = new DataSet();
  125. DataTable dtNew = new DataTable();
  126. DataColumn dc = null;
  127. dsNew.Tables.Add(dtNew);
  128. foreach (string s in columnList)
  129. {
  130. try
  131. {
  132. if(dsSource.Tables[0].Columns.Contains(s))
  133. dc = new DataColumn(s, dsSource.Tables[0].Columns[s].DataType);
  134. else
  135. dc = new DataColumn(s, System.Type.GetType("System.String"));
  136. dsNew.Tables[0].Columns.Add(dc);
  137. }
  138. catch
  139. {
  140. }
  141. }
  142. dsNew.Merge(dsSource, false, System.Data.MissingSchemaAction.Ignore);
  143. dsNew.AcceptChanges();
  144. return dsNew;
  145. }
  146. public static DataSet GetDataSetWithTbColumnAndSourceDataSet(DataSet dsSource, string[] columnList,string [] columnListCaption)
  147. {
  148. if (dsSource == null)
  149. return dsSource;
  150. if (columnList.Length == 0)
  151. return dsSource;
  152. DataSet dsNew = new DataSet();
  153. DataTable dtNew = new DataTable();
  154. DataColumn dc = null;
  155. dsNew.Tables.Add(dtNew);
  156. for (int i = 0; i < columnList.Length; i++)
  157. {
  158. try
  159. {
  160. dc = new DataColumn(columnList[i], dsSource.Tables[0].Columns[columnList[i]].DataType);
  161. dc.Caption = columnListCaption[i];
  162. dsNew.Tables[0].Columns.Add(dc);
  163. }
  164. catch
  165. {
  166. }
  167. }
  168. dsNew.Tables[0].Merge(dsSource.Tables[0], true, MissingSchemaAction.Ignore);
  169. dsNew.AcceptChanges();
  170. return dsNew;
  171. }
  172. /// <summary>
  173. /// 根据hs,字段名和标题名,以及源数据集,组合新的数据集并返回
  174. /// </summary>
  175. /// <param name="ds"></param>
  176. /// <param name="hs"></param>
  177. /// <returns></returns>
  178. public static DataSet GetDataSetWithTbColumnAndSourceDataSetWithCaption(DataSet ds, Hashtable hs)
  179. {
  180. if (hs == null || hs.Keys.Count == 0)
  181. return null;
  182. if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
  183. return null;
  184. DataSet dsNew = new DataSet();
  185. DataTable dtNew = new DataTable();
  186. DataColumn dc = null;
  187. dsNew.Tables.Add(dtNew);
  188. foreach (string s in hs.Keys)
  189. {
  190. try
  191. {
  192. dc = new DataColumn(s, ds.Tables[0].Columns[s].DataType);
  193. dc.Caption = hs[s].ToString();
  194. dsNew.Tables[0].Columns.Add(dc);
  195. }
  196. catch
  197. {
  198. }
  199. }
  200. dsNew.Tables[0].Merge(ds.Tables[0],true,MissingSchemaAction.Ignore);
  201. dsNew.AcceptChanges();
  202. return dsNew;
  203. }
  204. #endregion
  205. #region 根据现有的数据集sourceDs,和空集合targetDs,将sourceDs数据按同字段存入到targetDs
  206. /// <summary>
  207. /// 根据现有的数据集sourceDs,和空集合targetDs,将sourceDs数据按同字段存入到targetDs
  208. /// </summary>
  209. /// <param name="targetDs"></param>
  210. /// <param name="sourceDs"></param>
  211. public static void MergeDataSetFromSourceDsToTargetDsBySameColumnName(ref DataSet targetDs, DataSet sourceDs)
  212. {
  213. if (sourceDs == null || sourceDs.Tables.Count == 0 || sourceDs.Tables[0].Rows.Count == 0)
  214. return;
  215. if (targetDs == null)
  216. return;
  217. try
  218. {
  219. DataRow drTarget = null;
  220. // 循环表
  221. foreach (DataRow dr in sourceDs.Tables[0].Rows)
  222. {
  223. drTarget = targetDs.Tables[0].NewRow();
  224. // 循环获取列名
  225. foreach (DataColumn dc in targetDs.Tables[0].Columns)
  226. {
  227. try
  228. {
  229. // 相同列字段赋值
  230. drTarget[dc.ColumnName] = dr[dc.ColumnName];
  231. }
  232. catch
  233. {
  234. }
  235. }
  236. // 新增行数据
  237. targetDs.Tables[0].Rows.Add(drTarget);
  238. }
  239. targetDs.AcceptChanges();
  240. }
  241. catch
  242. {
  243. }
  244. }
  245. #endregion
  246. #region 根据现有的数据集sourceDs,和空集合targetDs,将sourceDs数据按同字段存入到targetDs
  247. /// <summary>
  248. /// 根据现有的数据集sourceDs,和空集合targetDs,将sourceDs数据按同字段存入到targetDs
  249. /// </summary>
  250. /// <param name="targetDs"></param>
  251. /// <param name="sourceDs"></param>
  252. public static DataSet GetDataSetWithSameColumnOfTargetDs(DataSet targetDs, DataSet sourceDs)
  253. {
  254. if (sourceDs == null || sourceDs.Tables.Count == 0 || sourceDs.Tables[0].Rows.Count == 0)
  255. return targetDs;
  256. if (targetDs == null)
  257. return null;
  258. DataSet dsReturn = targetDs.Clone();
  259. try
  260. {
  261. DataRow drTarget = null;
  262. // 循环表
  263. foreach (DataRow dr in sourceDs.Tables[0].Rows)
  264. {
  265. drTarget = dsReturn.Tables[0].NewRow();
  266. // 循环获取列名
  267. foreach (DataColumn dc in targetDs.Tables[0].Columns)
  268. {
  269. try
  270. {
  271. // 相同列字段赋值
  272. drTarget[dc.ColumnName] = dr[dc.ColumnName];
  273. }
  274. catch
  275. {
  276. }
  277. }
  278. // 新增行数据
  279. dsReturn.Tables[0].Rows.Add(drTarget);
  280. }
  281. dsReturn.AcceptChanges();
  282. }
  283. catch
  284. {
  285. }
  286. return dsReturn;
  287. }
  288. #endregion
  289. #region 从一个数据集中获取某列的值,并存入list
  290. /// <summary>
  291. /// 从一个数据集中获取某列的值,并存入list
  292. /// </summary>
  293. /// <param name="dsSource">数据源</param>
  294. /// <param name="columnName">列名,全部大写</param>
  295. /// <returns></returns>
  296. public static ArrayList GetColumnValueFromDataSourceByColumnName(DataSet dsSource, string columnName)
  297. {
  298. ArrayList list = new ArrayList();
  299. if (dsSource == null || dsSource.Tables.Count == 0 || dsSource.Tables[0].Rows.Count == 0 || columnName.Length == 0)
  300. return list;
  301. if (!dsSource.Tables[0].Columns.Contains(columnName.ToUpper()))
  302. return list;
  303. try
  304. {
  305. foreach (DataRow dr in dsSource.Tables[0].Rows)
  306. {
  307. try
  308. {
  309. list.Add(dr[columnName.ToUpper()].ToString());
  310. }
  311. catch
  312. {
  313. break;
  314. }
  315. }
  316. }
  317. catch
  318. {
  319. }
  320. return list;
  321. }
  322. public static List<String> GetColumnStringValueFromDataSourceByColumnName(DataSet dsSource, string columnName)
  323. {
  324. List<String> list = new List<string>();
  325. if (dsSource == null || dsSource.Tables.Count == 0 || dsSource.Tables[0].Rows.Count == 0 || columnName.Length == 0)
  326. return list;
  327. if (!dsSource.Tables[0].Columns.Contains(columnName.ToUpper()))
  328. return list;
  329. try
  330. {
  331. foreach (DataRow dr in dsSource.Tables[0].Rows)
  332. {
  333. try
  334. {
  335. list.Add(dr[columnName.ToUpper()].ToString());
  336. }
  337. catch
  338. {
  339. break;
  340. }
  341. }
  342. }
  343. catch
  344. {
  345. }
  346. return list;
  347. }
  348. #endregion
  349. #region 从一个ds中,按条件过滤数据
  350. public static DataSet GetDataSetByRowFilter( DataSet ds, string rowFilter)
  351. {
  352. try
  353. {
  354. DataSet dsTemp = new DataSet();
  355. DataSet dsReturn = new DataSet();
  356. if (dsTemp == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
  357. return ds;
  358. dsTemp = ds.Copy();
  359. DataView dv = dsTemp.Tables[0].DefaultView;
  360. dv.RowFilter = rowFilter;
  361. DataTable dt = dv.ToTable();
  362. dsReturn.Tables.Add(dt);
  363. return dsReturn;
  364. }
  365. catch
  366. {
  367. return null;
  368. }
  369. }
  370. #endregion
  371. #region 给ds排序
  372. /// <summary>
  373. /// 降序排一个ds
  374. /// </summary>
  375. /// <param name="dsSource">待排序ds</param>
  376. /// <param name="sortColumn">排序字段</param>
  377. /// <returns></returns>
  378. public static DataSet ReturnDsByDescOrderFromSourceDataSet(DataSet dsSource, string sortColumn)
  379. {
  380. DataSet ds = new DataSet();
  381. try
  382. {
  383. if (dsSource != null & dsSource.Tables.Count > 0)
  384. {
  385. if (!dsSource.Tables[0].Columns.Contains(sortColumn))
  386. return dsSource.Copy();
  387. if (dsSource.Tables[0].Rows.Count == 0)
  388. ds = dsSource.Clone();
  389. else
  390. {
  391. DataView dv = new DataView(dsSource.Tables[0]);
  392. dv.Sort = string.Format("{0} desc", sortColumn.ToUpper());
  393. ds.Clear();
  394. ds.Tables.Add(dv.ToTable());
  395. }
  396. }
  397. }
  398. catch
  399. {
  400. }
  401. return ds;
  402. }
  403. /// <summary>
  404. /// 升序排一个ds
  405. /// </summary>
  406. /// <param name="dsSource">待排序ds</param>
  407. /// <param name="sortColumn">排序字段</param>
  408. /// <returns></returns>
  409. public static DataSet ReturnDsByAscOrderFromSourceDataSet(DataSet dsSource, string sortColumn)
  410. {
  411. DataSet ds = new DataSet();
  412. try
  413. {
  414. if (dsSource != null & dsSource.Tables.Count > 0)
  415. {
  416. if (!dsSource.Tables[0].Columns.Contains(sortColumn))
  417. return dsSource.Copy();
  418. if (dsSource.Tables[0].Rows.Count == 0)
  419. ds = dsSource.Clone();
  420. else
  421. {
  422. DataView dv = new DataView(dsSource.Tables[0]);
  423. dv.Sort = string.Format("{0} asc", sortColumn.ToUpper());
  424. ds.Clear();
  425. ds.Tables.Add(dv.ToTable());
  426. }
  427. }
  428. }
  429. catch
  430. {
  431. }
  432. return ds;
  433. }
  434. #endregion
  435. }
  436. }