#region 獲取DataTable前幾條數(shù)據(jù) /// <summary> /// 獲取DataTable前幾條數(shù)據(jù) /// </summary> /// <param name="TopItem">前N條數(shù)據(jù)</param> /// <param name="oDT">源DataTable</param> /// <returns></returns> public static DataTable DtSelectTop(int TopItem, DataTable oDT) { if (oDT.Rows.Count < TopItem) return oDT; DataTable NewTable = oDT.Clone(); DataRow[] rows = oDT.Select("1=1"); for (int i = 0; i < TopItem; i++) { NewTable.ImportRow((DataRow)rows[i]); } return NewTable; } #endregion #region 獲取DataTable中指定列的數(shù)據(jù) /// <summary> /// 獲取DataTable中指定列的數(shù)據(jù) /// </summary> /// <param name="dt">數(shù)據(jù)源</param> /// <param name="tableName">新的DataTable的名詞</param> /// <param name="strColumns">指定的列名集合</param> /// <returns>返回新的DataTable</returns> public static DataTable GetTableColumn(DataTable dt, string tableName, params string[] strColumns) { DataTable dtn = new DataTable(); if (dt == null) { throw new ArgumentNullException("參數(shù)dt不能為null"); } try { dtn = dt.DefaultView.ToTable(tableName, true, strColumns); } catch (Exception e) { throw new Exception(e.Message); } return dtn; } #endregion
using System; using System.Collections.Generic; using System.Linq; using System.Data; using System.Collections; using System.Text; namespace GuanEasy { /// <summary> /// DataSet助手 /// </summary> public class DataSetHelper { private class FieldInfo { public string RelationName; public string FieldName; public string FieldAlias; public string Aggregate; } private DataSet ds; private ArrayList m_FieldInfo; private string m_FieldList; private ArrayList GroupByFieldInfo; private string GroupByFieldList; public DataSet DataSet { get { return ds; } } #region Construction public DataSetHelper() { ds = null; } public DataSetHelper(ref DataSet dataSet) { ds = dataSet; } #endregion #region Private Methods private bool ColumnEqual(object objectA, object objectB) { if ( objectA == DBNull.Value && objectB == DBNull.Value ) { return true; } if ( objectA == DBNull.Value || objectB == DBNull.Value ) { return false; } return ( objectA.Equals( objectB ) ); } private bool RowEqual(DataRow rowA, DataRow rowB, DataColumnCollection columns) { bool result = true; for ( int i = 0; i < columns.Count; i++ ) { result &= ColumnEqual( rowA[ columns[ i ].ColumnName ], rowB[ columns[ i ].ColumnName ] ); } return result; } private void ParseFieldList(string fieldList, bool allowRelation) { if ( m_FieldList == fieldList ) { return; } m_FieldInfo = new ArrayList(); m_FieldList = fieldList; FieldInfo Field; string[] FieldParts; string[] Fields = fieldList.Split( ',' ); for ( int i = 0; i <= Fields.Length - 1; i++ ) { Field = new FieldInfo(); FieldParts = Fields[ i ].Trim().Split( ' ' ); switch ( FieldParts.Length ) { case 1: //to be set at the end of the loop break; case 2: Field.FieldAlias = FieldParts[ 1 ]; break; default: return; } FieldParts = FieldParts[ 0 ].Split( '.' ); switch ( FieldParts.Length ) { case 1: Field.FieldName = FieldParts[ 0 ]; break; case 2: if ( allowRelation == false ) { return; } Field.RelationName = FieldParts[ 0 ].Trim(); Field.FieldName = FieldParts[ 1 ].Trim(); break; default: return; } if ( Field.FieldAlias == null ) { Field.FieldAlias = Field.FieldName; } m_FieldInfo.Add( Field ); } } private DataTable CreateTable(string tableName, DataTable sourceTable, string fieldList) { DataTable dt; if ( fieldList.Trim() == "" ) { dt = sourceTable.Clone(); dt.TableName = tableName; } else { dt = new DataTable( tableName ); ParseFieldList( fieldList, false ); DataColumn dc; foreach ( FieldInfo Field in m_FieldInfo ) { dc = sourceTable.Columns[ Field.FieldName ]; DataColumn column = new DataColumn(); column.ColumnName = Field.FieldAlias; column.DataType = dc.DataType; column.MaxLength = dc.MaxLength; column.Expression = dc.Expression; dt.Columns.Add( column ); } } if ( ds != null ) { ds.Tables.Add( dt ); } return dt; } private void InsertInto(DataTable destTable, DataTable sourceTable, string fieldList, string rowFilter, string sort) { ParseFieldList( fieldList, false ); DataRow[] rows = sourceTable.Select( rowFilter, sort ); DataRow destRow; foreach ( DataRow sourceRow in rows ) { destRow = destTable.NewRow(); if ( fieldList == "" ) { foreach ( DataColumn dc in destRow.Table.Columns ) { if ( dc.Expression == "" ) { destRow[ dc ] = sourceRow[ dc.ColumnName ]; } } } else { foreach ( FieldInfo field in m_FieldInfo ) { destRow[ field.FieldAlias ] = sourceRow[ field.FieldName ]; } } destTable.Rows.Add( destRow ); } } private void ParseGroupByFieldList(string FieldList) { if ( GroupByFieldList == FieldList ) { return; } GroupByFieldInfo = new ArrayList(); FieldInfo Field; string[] FieldParts; string[] Fields = FieldList.Split( ',' ); for ( int i = 0; i <= Fields.Length - 1; i++ ) { Field = new FieldInfo(); FieldParts = Fields[ i ].Trim().Split( ' ' ); switch ( FieldParts.Length ) { case 1: //to be set at the end of the loop break; case 2: Field.FieldAlias = FieldParts[ 1 ]; break; default: return; } FieldParts = FieldParts[ 0 ].Split( '(' ); switch ( FieldParts.Length ) { case 1: Field.FieldName = FieldParts[ 0 ]; break; case 2: Field.Aggregate = FieldParts[ 0 ].Trim().ToLower(); Field.FieldName = FieldParts[ 1 ].Trim( ' ', ')' ); break; default: return; } if ( Field.FieldAlias == null ) { if ( Field.Aggregate == null ) { Field.FieldAlias = Field.FieldName; } else { Field.FieldAlias = Field.Aggregate + "of" + Field.FieldName; } } GroupByFieldInfo.Add( Field ); } GroupByFieldList = FieldList; } private DataTable CreateGroupByTable(string tableName, DataTable sourceTable, string fieldList) { if ( fieldList == null || fieldList.Length == 0 ) { return sourceTable.Clone(); } else { DataTable dt = new DataTable( tableName ); ParseGroupByFieldList( fieldList ); foreach ( FieldInfo Field in GroupByFieldInfo ) { DataColumn dc = sourceTable.Columns[ Field.FieldName ]; if ( Field.Aggregate == null ) { dt.Columns.Add( Field.FieldAlias, dc.DataType, dc.Expression ); } else { dt.Columns.Add( Field.FieldAlias, dc.DataType ); } } if ( ds != null ) { ds.Tables.Add( dt ); } return dt; } } private void InsertGroupByInto(DataTable destTable, DataTable sourceTable, string fieldList, string rowFilter, string groupBy) { if ( fieldList == null || fieldList.Length == 0 ) { return; } ParseGroupByFieldList( fieldList ); ParseFieldList( groupBy, false ); DataRow[] rows = sourceTable.Select( rowFilter, groupBy ); DataRow lastSourceRow = null, destRow = null; bool sameRow; int rowCount = 0; foreach ( DataRow sourceRow in rows ) { sameRow = false; if ( lastSourceRow != null ) { sameRow = true; foreach ( FieldInfo Field in m_FieldInfo ) { if ( !ColumnEqual