| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490 |
- using System;
- using System.Reflection;
- using System.Collections;
- using System.Resources;
- using System.Windows.Forms;
- using System.Data;
- using System.Collections.Generic;
- namespace Core.StlMes.Client.Sale.SaleFundMgt.Util
- {
- public class DataSetUtil
- {
- #region 获取一个ds的行数,为null or table==0 or rows = 0 都返回0
- /// <summary>
- /// 获取一个ds的行数
- /// </summary>
- /// <param name="ds"></param>
- /// <returns></returns>
- public static int Count(DataSet ds)
- {
- try
- {
- return ds.Tables[0].Rows.Count;
- }
- catch
- {
- }
- return 0;
- }
- #endregion
- #region 获取整个系统中出现的为number类型的字段,并返回hstb
- public static Hashtable GetNumberColumnByHsTb()
- {
- string[] columnName = new string[]
- {
- "QUANTITY","WEIGHT","PRICE","MONEY","BASEPRICE",
- "ALLWEIGHT","TRANSMONEY","INVOICETRANSMONEY","BPRICE","PLY",
- "WIDTH","LENGTH","ZYFMONEY","TESTPRICE","TRANSMONEYQC",
- "TRANSMONEYSY1","CARFMONEY","FCPRICE"
- };
- Hashtable hs = new Hashtable();
- foreach (string s in columnName)
- {
- try
- {
- hs.Add(s, "System.Decimal");
- }
- catch
- {
- }
- }
- return hs;
- }
- #endregion
- #region 根据传入的字段组合一个ds,并返回,ds 字段默认类型string
- /// <summary>
- /// 根据传入的字段组合一个ds,并返回,ds 字段默认类型string
- /// </summary>
- /// <param name="columnName"></param>
- /// <returns></returns>
- public static DataSet GetDataSetByTbColumn(string[] columnName)
- {
- if (columnName == null || columnName.Length == 0)
- return null;
- DataSet ds = new DataSet();
- DataTable dt = new DataTable();
- DataColumn column = null;
- foreach (string s in columnName)
- {
- column = new DataColumn(s, System.Type.GetType("System.String"));
- dt.Columns.Add(s);
- }
- ds.Tables.Add(dt);
- return ds;
- }
- #endregion
- #region 根据字段名称以及字段类型组合ds,没有指定字段类型的默认string
- /// <summary>
- /// 根据字段名称以及字段类型组合ds,没有指定字段类型的默认string
- /// </summary>
- /// <param name="columnName"></param>
- /// <param name="hs"></param>
- /// <returns></returns>
- public static DataSet GetDataSetByTbColumn(string[] columnName, Hashtable hs)
- {
- if (columnName == null || columnName.Length == 0 || hs == null)
- return null;
- DataSet ds = new DataSet();
- DataTable dt = new DataTable();
- DataColumn column = null;
- foreach (string s in columnName)
- {
- try
- {
- if (hs.Contains(s))
- {
- column = new DataColumn(s, System.Type.GetType(hs[s].ToString()));
- }
- else
- {
- column = new DataColumn(s, System.Type.GetType("System.String"));
- }
- dt.Columns.Add(s);
- }
- catch
- {
- }
- }
- ds.Tables.Add(dt);
- return ds;
- }
- /// <summary>
- /// 按给定的字段列表,columnList,组合一个ds,名称和dssource相同,类型从dssource查找
- /// 并填充
- /// </summary>
- /// <param name="dsSource"></param>
- /// <param name="columnList"></param>
- /// <returns></returns>
- public static DataSet GetDataSetWithTbColumnAndSourceDataSet(DataSet dsSource,string [] columnList)
- {
- if (Util.DataSetUtil.Count(dsSource) == 0)
- return dsSource;
- if (columnList.Length == 0)
- return dsSource;
- DataSet dsNew = new DataSet();
- DataTable dtNew = new DataTable();
- DataColumn dc = null;
- dsNew.Tables.Add(dtNew);
- foreach (string s in columnList)
- {
- try
- {
- if(dsSource.Tables[0].Columns.Contains(s))
- dc = new DataColumn(s, dsSource.Tables[0].Columns[s].DataType);
- else
- dc = new DataColumn(s, System.Type.GetType("System.String"));
- dsNew.Tables[0].Columns.Add(dc);
- }
- catch
- {
- }
- }
- dsNew.Merge(dsSource, false, System.Data.MissingSchemaAction.Ignore);
- dsNew.AcceptChanges();
- return dsNew;
- }
- public static DataSet GetDataSetWithTbColumnAndSourceDataSet(DataSet dsSource, string[] columnList,string [] columnListCaption)
- {
- if (dsSource == null)
- return dsSource;
- if (columnList.Length == 0)
- return dsSource;
- DataSet dsNew = new DataSet();
- DataTable dtNew = new DataTable();
- DataColumn dc = null;
- dsNew.Tables.Add(dtNew);
- for (int i = 0; i < columnList.Length; i++)
- {
- try
- {
- dc = new DataColumn(columnList[i], dsSource.Tables[0].Columns[columnList[i]].DataType);
- dc.Caption = columnListCaption[i];
- dsNew.Tables[0].Columns.Add(dc);
- }
- catch
- {
- }
- }
- dsNew.Tables[0].Merge(dsSource.Tables[0], true, MissingSchemaAction.Ignore);
- dsNew.AcceptChanges();
- return dsNew;
- }
- /// <summary>
- /// 根据hs,字段名和标题名,以及源数据集,组合新的数据集并返回
- /// </summary>
- /// <param name="ds"></param>
- /// <param name="hs"></param>
- /// <returns></returns>
- public static DataSet GetDataSetWithTbColumnAndSourceDataSetWithCaption(DataSet ds, Hashtable hs)
- {
- if (hs == null || hs.Keys.Count == 0)
- return null;
- if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
- return null;
- DataSet dsNew = new DataSet();
- DataTable dtNew = new DataTable();
- DataColumn dc = null;
- dsNew.Tables.Add(dtNew);
- foreach (string s in hs.Keys)
- {
- try
- {
- dc = new DataColumn(s, ds.Tables[0].Columns[s].DataType);
- dc.Caption = hs[s].ToString();
- dsNew.Tables[0].Columns.Add(dc);
- }
- catch
- {
- }
- }
- dsNew.Tables[0].Merge(ds.Tables[0],true,MissingSchemaAction.Ignore);
- dsNew.AcceptChanges();
- return dsNew;
- }
- #endregion
- #region 根据现有的数据集sourceDs,和空集合targetDs,将sourceDs数据按同字段存入到targetDs
- /// <summary>
- /// 根据现有的数据集sourceDs,和空集合targetDs,将sourceDs数据按同字段存入到targetDs
- /// </summary>
- /// <param name="targetDs"></param>
- /// <param name="sourceDs"></param>
- public static void MergeDataSetFromSourceDsToTargetDsBySameColumnName(ref DataSet targetDs, DataSet sourceDs)
- {
- if (sourceDs == null || sourceDs.Tables.Count == 0 || sourceDs.Tables[0].Rows.Count == 0)
- return;
- if (targetDs == null)
- return;
- try
- {
- DataRow drTarget = null;
- // 循环表
- foreach (DataRow dr in sourceDs.Tables[0].Rows)
- {
- drTarget = targetDs.Tables[0].NewRow();
- // 循环获取列名
- foreach (DataColumn dc in targetDs.Tables[0].Columns)
- {
- try
- {
- // 相同列字段赋值
- drTarget[dc.ColumnName] = dr[dc.ColumnName];
- }
- catch
- {
- }
- }
- // 新增行数据
- targetDs.Tables[0].Rows.Add(drTarget);
- }
- targetDs.AcceptChanges();
- }
- catch
- {
- }
- }
- #endregion
- #region 根据现有的数据集sourceDs,和空集合targetDs,将sourceDs数据按同字段存入到targetDs
- /// <summary>
- /// 根据现有的数据集sourceDs,和空集合targetDs,将sourceDs数据按同字段存入到targetDs
- /// </summary>
- /// <param name="targetDs"></param>
- /// <param name="sourceDs"></param>
- public static DataSet GetDataSetWithSameColumnOfTargetDs(DataSet targetDs, DataSet sourceDs)
- {
- if (sourceDs == null || sourceDs.Tables.Count == 0 || sourceDs.Tables[0].Rows.Count == 0)
- return targetDs;
-
- if (targetDs == null)
- return null;
- DataSet dsReturn = targetDs.Clone();
- try
- {
- DataRow drTarget = null;
- // 循环表
- foreach (DataRow dr in sourceDs.Tables[0].Rows)
- {
- drTarget = dsReturn.Tables[0].NewRow();
- // 循环获取列名
- foreach (DataColumn dc in targetDs.Tables[0].Columns)
- {
- try
- {
- // 相同列字段赋值
- drTarget[dc.ColumnName] = dr[dc.ColumnName];
- }
- catch
- {
- }
- }
- // 新增行数据
- dsReturn.Tables[0].Rows.Add(drTarget);
- }
- dsReturn.AcceptChanges();
- }
- catch
- {
- }
- return dsReturn;
- }
- #endregion
- #region 从一个数据集中获取某列的值,并存入list
- /// <summary>
- /// 从一个数据集中获取某列的值,并存入list
- /// </summary>
- /// <param name="dsSource">数据源</param>
- /// <param name="columnName">列名,全部大写</param>
- /// <returns></returns>
- public static ArrayList GetColumnValueFromDataSourceByColumnName(DataSet dsSource, string columnName)
- {
- ArrayList list = new ArrayList();
- if (dsSource == null || dsSource.Tables.Count == 0 || dsSource.Tables[0].Rows.Count == 0 || columnName.Length == 0)
- return list;
- if (!dsSource.Tables[0].Columns.Contains(columnName.ToUpper()))
- return list;
- try
- {
- foreach (DataRow dr in dsSource.Tables[0].Rows)
- {
- try
- {
- list.Add(dr[columnName.ToUpper()].ToString());
- }
- catch
- {
- break;
- }
- }
- }
- catch
- {
- }
- return list;
- }
- public static List<String> GetColumnStringValueFromDataSourceByColumnName(DataSet dsSource, string columnName)
- {
- List<String> list = new List<string>();
- if (dsSource == null || dsSource.Tables.Count == 0 || dsSource.Tables[0].Rows.Count == 0 || columnName.Length == 0)
- return list;
- if (!dsSource.Tables[0].Columns.Contains(columnName.ToUpper()))
- return list;
- try
- {
- foreach (DataRow dr in dsSource.Tables[0].Rows)
- {
- try
- {
- list.Add(dr[columnName.ToUpper()].ToString());
- }
- catch
- {
- break;
- }
- }
- }
- catch
- {
- }
- return list;
- }
- #endregion
- #region 从一个ds中,按条件过滤数据
- public static DataSet GetDataSetByRowFilter( DataSet ds, string rowFilter)
- {
- try
- {
- DataSet dsTemp = new DataSet();
- DataSet dsReturn = new DataSet();
- if (dsTemp == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
- return ds;
- dsTemp = ds.Copy();
- DataView dv = dsTemp.Tables[0].DefaultView;
- dv.RowFilter = rowFilter;
- DataTable dt = dv.ToTable();
- dsReturn.Tables.Add(dt);
- return dsReturn;
- }
- catch
- {
- return null;
- }
- }
- #endregion
- #region 给ds排序
- /// <summary>
- /// 降序排一个ds
- /// </summary>
- /// <param name="dsSource">待排序ds</param>
- /// <param name="sortColumn">排序字段</param>
- /// <returns></returns>
- public static DataSet ReturnDsByDescOrderFromSourceDataSet(DataSet dsSource, string sortColumn)
- {
- DataSet ds = new DataSet();
- try
- {
- if (dsSource != null & dsSource.Tables.Count > 0)
- {
- if (!dsSource.Tables[0].Columns.Contains(sortColumn))
- return dsSource.Copy();
- if (dsSource.Tables[0].Rows.Count == 0)
- ds = dsSource.Clone();
- else
- {
- DataView dv = new DataView(dsSource.Tables[0]);
- dv.Sort = string.Format("{0} desc", sortColumn.ToUpper());
- ds.Clear();
- ds.Tables.Add(dv.ToTable());
- }
- }
- }
- catch
- {
- }
- return ds;
- }
- /// <summary>
- /// 升序排一个ds
- /// </summary>
- /// <param name="dsSource">待排序ds</param>
- /// <param name="sortColumn">排序字段</param>
- /// <returns></returns>
- public static DataSet ReturnDsByAscOrderFromSourceDataSet(DataSet dsSource, string sortColumn)
- {
- DataSet ds = new DataSet();
- try
- {
- if (dsSource != null & dsSource.Tables.Count > 0)
- {
- if (!dsSource.Tables[0].Columns.Contains(sortColumn))
- return dsSource.Copy();
- if (dsSource.Tables[0].Rows.Count == 0)
- ds = dsSource.Clone();
- else
- {
- DataView dv = new DataView(dsSource.Tables[0]);
- dv.Sort = string.Format("{0} asc", sortColumn.ToUpper());
- ds.Clear();
- ds.Tables.Add(dv.ToTable());
- }
- }
- }
- catch
- {
- }
- return ds;
- }
- #endregion
- }
- }
|