#region DataTable筛选,排序返回符合条件行组成的新DataTable或直接用DefaultView按条件返回  
    /// <summary>  
    /// DataTable筛选,排序返回符合条件行组成的新DataTable或直接用DefaultView按条件返回  
    /// eg:SortExprDataTable(dt,"Sex='男'","Time Desc",1)  
    /// </summary>  
    /// <param name="dt">传入的DataTable</param>  
    /// <param name="strExpr">筛选条件</param>  
    /// <param name="strSort">排序条件</param>  
    /// <param name="mode">1,直接用DefaultView按条件返回,效率较高;2,DataTable筛选,排序返回符合条件行组成的新DataTable</param>  
    public static DataTable SortDataTable(DataTable dt, string strExpr, string strSort, int mode)  
    {  
        switch (mode)  
        {  
            case 1:  
                //方法一 直接用DefaultView按条件返回  
                dt.DefaultView.RowFilter = strExpr;  
                dt.DefaultView.Sort = strSort;  
                return dt;  
            case 2:  
                //方法二 DataTable筛选,排序返回符合条件行组成的新DataTable  
                DataTable dt1 = new DataTable();  
                DataRow[] GetRows = dt.Select(strExpr, strSort);  
                //复制DataTable dt结构不包含数据  
                dt1 = dt.Clone();  
                foreach (DataRow row in GetRows)  
                {  
                    dt1.Rows.Add(row.ItemArray);  
                }  
                return dt1;  
            default:  
                return dt;  
        }  
    }  
    #endregion

#region 获取DataTable前几条数据  
    /// <summary>  
    /// 获取DataTable前几条数据  
    /// </summary>  
    /// <param name="TopItem">前N条数据</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中指定列的数据  
    /// <summary>  
    /// 获取DataTable中指定列的数据  
    /// </summary>  
    /// <param name="dt">数据源</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("参数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( lastSourceRow[ Field.FieldName ], sourceRow[ Field.FieldName ] ) )   
                            {   
                                sameRow = false;   
                                break;   
                            }   
                        }   
                        if ( !sameRow )   
                        {   
                            destTable.Rows.Add( destRow );   
                        }   
                    }   
                    if ( !sameRow )   
                    {   
                        destRow = destTable.NewRow();   
                        rowCount = 0;   
                    }   
                    rowCount += 1;   
                    foreach ( FieldInfo field in GroupByFieldInfo )   
                    {   
                        switch ( field.Aggregate.ToLower() )   
                        {   
                            case null:    
                            case "":   
                            case "last":   
                                destRow[ field.FieldAlias ] = sourceRow[ field.FieldName ];   
                                break;   
                            case "first":   
                                if ( rowCount == 1 )   
                                {   
                                    destRow[ field.FieldAlias ] = sourceRow[ field.FieldName ];   
                                }   
                                break;   
                            case "count":   
                                destRow[ field.FieldAlias ] = rowCount;   
                                break;   
                            case "sum":   
                                destRow[ field.FieldAlias ] = Add( destRow[ field.FieldAlias ], sourceRow[ field.FieldName ] );   
                                break;   
                            case "max":   
                                destRow[ field.FieldAlias ] = Max( destRow[ field.FieldAlias ], sourceRow[ field.FieldName ] );   
                                break;   
                            case "min":   
                                if ( rowCount == 1 )   
                                {   
                                    destRow[ field.FieldAlias ] = sourceRow[ field.FieldName ];   
                                }   
                                else  
                                {   
                                    destRow[ field.FieldAlias ] = Min( destRow[ field.FieldAlias ], sourceRow[ field.FieldName ] );   
                                }   
                                break;   
                        }   
                    }   
                    lastSourceRow = sourceRow;   
                }   
                if ( destRow != null )   
                {   
                    destTable.Rows.Add( destRow );   
                }   
            }   
        
            private object Min(object a, object b)   
            {   
                if ( ( a is DBNull ) || ( b is DBNull ) )   
                {   
                    return DBNull.Value;   
                }   
                if ( ( (IComparable) a ).CompareTo( b ) == -1 )   
                {   
                    return a;   
                }   
                else  
                {   
                    return b;   
                }   
            }   
        
            private object Max(object a, object b)   
            {   
                if ( a is DBNull )   
                {   
                    return b;   
                }   
                if ( b is DBNull )   
                {   
                    return a;   
                }   
                if ( ( (IComparable) a ).CompareTo( b ) == 1 )   
                {   
                    return a;   
                }   
                else  
                {   
                    return b;   
                }   
            }   
        
            private object Add(object a, object b)   
            {   
                if ( a is DBNull )   
                {   
                    return b;   
                }   
                if ( b is DBNull )   
                {   
                    return a;   
                }   
                return ( (decimal) a + (decimal) b );   
            }   
        
            private DataTable CreateJoinTable(string tableName, DataTable sourceTable, string fieldList)   
            {   
                if ( fieldList == null )   
                {   
                    return sourceTable.Clone();   
                }   
                else  
                {   
                    DataTable dt = new DataTable( tableName );   
                    ParseFieldList( fieldList, true );   
                    foreach ( FieldInfo field in m_FieldInfo )   
                    {   
                        if ( field.RelationName == null )   
                        {   
                            DataColumn dc = sourceTable.Columns[ field.FieldName ];   
                            dt.Columns.Add( dc.ColumnName, dc.DataType, dc.Expression );   
                        }   
                        else  
                        {   
                            DataColumn dc = sourceTable.ParentRelations[ field.RelationName ].ParentTable.Columns[ field.FieldName ];   
                            dt.Columns.Add( dc.ColumnName, dc.DataType, dc.Expression );   
                        }   
                    }   
                    if ( ds != null )   
                    {   
                        ds.Tables.Add( dt );   
                    }   
                    return dt;   
                }   
            }   
        
            private void InsertJoinInto(DataTable destTable, DataTable sourceTable,   
                                        string fieldList, string rowFilter, string sort)   
            {   
                if ( fieldList == null )   
                {   
                    return;   
                }   
                else  
                {   
                    ParseFieldList( fieldList, true );   
                    DataRow[] Rows = sourceTable.Select( rowFilter, sort );   
                    foreach ( DataRow SourceRow in Rows )   
                    {   
                        DataRow DestRow = destTable.NewRow();   
                        foreach ( FieldInfo Field in m_FieldInfo )   
                        {   
                            if ( Field.RelationName == null )   
                            {   
                                DestRow[ Field.FieldName ] = SourceRow[ Field.FieldName ];   
                            }   
                            else  
                            {   
                                DataRow ParentRow = SourceRow.GetParentRow( Field.RelationName );   
                                DestRow[ Field.FieldName ] = ParentRow[ Field.FieldName ];   
                            }   
                        }   
                        destTable.Rows.Add( DestRow );   
                    }   
                }   
            }   
       
            #endregion   
       
            #region SelectDistinct / Distinct   
        
            /// <summary>   
            /// 按照fieldName从sourceTable中选择出不重复的行,   
            /// 相当于select distinct fieldName from sourceTable   
            /// </summary>   
            /// <param name="tableName">表名</param>   
            /// <param name="sourceTable">源DataTable</param>   
            /// <param name="fieldName">列名</param>   
            /// <returns>一个新的不含重复行的DataTable,列只包括fieldName指明的列</returns>   
            public DataTable SelectDistinct(string tableName, DataTable sourceTable, string fieldName)   
            {   
                DataTable dt = new DataTable( tableName );   
                dt.Columns.Add( fieldName, sourceTable.Columns[ fieldName ].DataType );   
        
                object lastValue = null;   
                foreach ( DataRow dr in sourceTable.Select( "", fieldName ) )   
                {   
                    if ( lastValue == null || !( ColumnEqual( lastValue, dr[ fieldName ] ) ) )   
                    {   
                        lastValue = dr[ fieldName ];   
                        dt.Rows.Add( new object[]{lastValue} );   
                    }   
                }   
                if ( ds != null && !ds.Tables.Contains( tableName ) )   
                {   
                    ds.Tables.Add( dt );   
                }   
                return dt;   
            }   
        
            /// <summary>   
            /// 按照fieldName从sourceTable中选择出不重复的行,   
            /// 相当于select distinct fieldName1,fieldName2,,fieldNamen from sourceTable   
            /// </summary>   
            /// <param name="tableName">表名</param>   
            /// <param name="sourceTable">源DataTable</param>   
            /// <param name="fieldNames">列名数组</param>   
            /// <returns>一个新的不含重复行的DataTable,列只包括fieldNames中指明的列</returns>   
            public DataTable SelectDistinct(string tableName, DataTable sourceTable, string[] fieldNames)   
            {   
                DataTable dt = new DataTable( tableName );   
                object[] values = new object[fieldNames.Length];   
                string fields = "";   
                for ( int i = 0; i < fieldNames.Length; i++ )   
                {   
                    dt.Columns.Add( fieldNames[ i ], sourceTable.Columns[ fieldNames[ i ] ].DataType );   
                    fields += fieldNames[ i ] + ",";   
                }   
                fields = fields.Remove( fields.Length - 1, 1 );   
                DataRow lastRow = null;   
                foreach ( DataRow dr in sourceTable.Select( "", fields ) )   
                {   
                    if ( lastRow == null || !( RowEqual( lastRow, dr, dt.Columns ) ) )   
                    {   
                        lastRow = dr;   
                        for ( int i = 0; i < fieldNames.Length; i++ )   
                        {   
                            values[ i ] = dr[ fieldNames[ i ] ];   
                        }   
                        dt.Rows.Add( values );   
                    }   
                }   
                if ( ds != null && !ds.Tables.Contains( tableName ) )   
                {   
                    ds.Tables.Add( dt );   
                }   
                return dt;   
            }   
        
            /// <summary>   
            /// 按照fieldName从sourceTable中选择出不重复的行,   
            /// 并且包含sourceTable中所有的列。   
            /// </summary>   
            /// <param name="tableName">表名</param>   
            /// <param name="sourceTable">源表</param>   
            /// <param name="fieldName">字段</param>   
            /// <returns>一个新的不含重复行的DataTable</returns>   
            public DataTable Distinct(string tableName, DataTable sourceTable, string fieldName)   
            {   
                DataTable dt = sourceTable.Clone();   
                dt.TableName = tableName;   
        
                object lastValue = null;   
                foreach ( DataRow dr in sourceTable.Select( "", fieldName ) )   
                {   
                    if ( lastValue == null || !( ColumnEqual( lastValue, dr[ fieldName ] ) ) )   
                    {   
                        lastValue = dr[ fieldName ];   
                        dt.Rows.Add( dr.ItemArray );   
                    }   
                }   
                if ( ds != null && !ds.Tables.Contains( tableName ) )   
                {   
                    ds.Tables.Add( dt );   
                }   
                return dt;   
            }   
        
            /// <summary>   
            /// 按照fieldNames从sourceTable中选择出不重复的行,   
            /// 并且包含sourceTable中所有的列。   
            /// </summary>   
            /// <param name="tableName">表名</param>   
            /// <param name="sourceTable">源表</param>   
            /// <param name="fieldNames">字段</param>   
            /// <returns>一个新的不含重复行的DataTable</returns>   
            public DataTable Distinct(string tableName, DataTable sourceTable, string[] fieldNames)   
            {   
                DataTable dt = sourceTable.Clone();   
                dt.TableName = tableName;   
                string fields = "";   
                for ( int i = 0; i < fieldNames.Length; i++ )   
                {   
                    fields += fieldNames[ i ] + ",";   
                }   
                fields = fields.Remove( fields.Length - 1, 1 );   
                DataRow lastRow = null;   
                foreach ( DataRow dr in sourceTable.Select( "", fields ) )   
                {   
                    if ( lastRow == null || !( RowEqual( lastRow, dr, dt.Columns ) ) )   
                    {   
                        lastRow = dr;   
                        dt.Rows.Add( dr.ItemArray );   
                    }   
                }   
                if ( ds != null && !ds.Tables.Contains( tableName ) )   
                {   
                    ds.Tables.Add( dt );   
                }   
                return dt;   
            }   
       
            #endregion   
       
            #region Select Table Into   
        
            /// <summary>   
            /// 按sort排序,按rowFilter过滤sourceTable,   
            /// 复制fieldList中指明的字段的数据到新DataTable,并返回之   
            /// </summary>   
            /// <param name="tableName">表名</param>   
            /// <param name="sourceTable">源表</param>   
            /// <param name="fieldList">字段列表</param>   
            /// <param name="rowFilter">过滤条件</param>   
            /// <param name="sort">排序</param>   
            /// <returns>新DataTable</returns>   
            public DataTable SelectInto(string tableName, DataTable sourceTable,   
                                        string fieldList, string rowFilter, string sort)   
            {   
                DataTable dt = CreateTable( tableName, sourceTable, fieldList );   
                InsertInto( dt, sourceTable, fieldList, rowFilter, sort );   
                return dt;   
            }   
       
            #endregion   
       
            #region Group By Table   
        
            public DataTable SelectGroupByInto(string tableName, DataTable sourceTable, string fieldList,   
                                               string rowFilter, string groupBy)   
            {   
                DataTable dt = CreateGroupByTable( tableName, sourceTable, fieldList );   
                InsertGroupByInto( dt, sourceTable, fieldList, rowFilter, groupBy );   
                return dt;   
            }   
       
            #endregion   
       
            #region Join Tables   
        
            public DataTable SelectJoinInto(string tableName, DataTable sourceTable, string fieldList, string rowFilter, string sort)   
            {   
                DataTable dt = CreateJoinTable( tableName, sourceTable, fieldList );   
                InsertJoinInto( dt, sourceTable, fieldList, rowFilter, sort );   
                return dt;   
            }   
       
            #endregion   
       
            #region Create Table   
        
            public DataTable CreateTable(string tableName, string fieldList)   
            {   
                DataTable dt = new DataTable( tableName );   
                DataColumn dc;   
                string[] Fields = fieldList.Split( ',' );   
                string[] FieldsParts;   
                string Expression;   
                foreach ( string Field in Fields )   
                {   
                    FieldsParts = Field.Trim().Split( " ".ToCharArray(), 3 ); // allow for spaces in the expression   
                    // add fieldname and datatype   
                    if ( FieldsParts.Length == 2 )   
                    {   
                        dc = dt.Columns.Add( FieldsParts[ 0 ].Trim(), Type.GetType( "System." + FieldsParts[ 1 ].Trim(), true, true ) );   
                        dc.AllowDBNull = true;   
                    }   
                    else if ( FieldsParts.Length == 3 ) // add fieldname, datatype, and expression   
                    {   
                        Expression = FieldsParts[ 2 ].Trim();   
                        if ( Expression.ToUpper() == "REQUIRED" )   
                        {   
                            dc = dt.Columns.Add( FieldsParts[ 0 ].Trim(), Type.GetType( "System." + FieldsParts[ 1 ].Trim(), true, true ) );   
                            dc.AllowDBNull = false;   
                        }   
                        else  
                        {   
                            dc = dt.Columns.Add( FieldsParts[ 0 ].Trim(), Type.GetType( "System." + FieldsParts[ 1 ].Trim(), true, true ), Expression );   
                        }   
                    }   
                    else  
                    {   
                        return null;   
                    }   
                }   
                if ( ds != null )   
                {   
                    ds.Tables.Add( dt );   
                }   
                return dt;   
            }   
        
            public DataTable CreateTable(string tableName, string fieldList, string keyFieldList)   
            {   
                DataTable dt = CreateTable( tableName, fieldList );   
                string[] KeyFields = keyFieldList.Split( ',' );   
                if ( KeyFields.Length > 0 )   
                {   
                    DataColumn[] KeyFieldColumns = new DataColumn[KeyFields.Length];   
                    int i;   
                    for ( i = 1; i == KeyFields.Length - 1; ++i )   
                    {   
                        KeyFieldColumns[ i ] = dt.Columns[ KeyFields[ i ].Trim() ];   
                    }   
                    dt.PrimaryKey = KeyFieldColumns;   
                }   
                return dt;   
            }   
       
            #endregion   
        }   
    }

DataTable相关操作,筛选,取前N条数据,去重复行,获取指定列数据的更多相关文章

  1. DataTable相关操作,筛选,取前N条数据,获取指定列数据

    DataTable相关操作,筛选,取前N条数据,获取指定列数据2013-03-12 14:50 by Miracle520, 2667 阅读, 0 评论, 收藏, 编辑 1 #region DataT ...

  2. ORACLE/MYSQL/DB2等不同数据库取前几条记录

    选取数据库中记录的操作是最基础最频繁的,但往往实际应用中不会这么简单,会在选取记录的时候加上一些条件,比如取前几条记录,下面就总结了如何在ORACLE/MYSQL/DB2等一些热门数据库中执行取前几条 ...

  3. MySql多表关联,根据某列取前N条记录问题

    近来遇到一个问题:“MySql多表关联,根据某列取前N条记录”. 刚开始一直在想,SQL语句是否可以做到直接查询出来,但几经折磨,还是没能写出SQL语句,-------如果有大牛的话,望指点迷津.我把 ...

  4. MSSQL—按照某一列分组后取前N条记录

    以前在开发的时候遇到过一个需求,就是要按照某一列进行分组后取前几条数据,今天又有同事碰到了,帮解决了之后顺便写一篇博客记录一下. 首先先建一个基础数据表,代码如下: IF OBJECT_ID(N'Te ...

  5. oracle 取前10条记录

    1.oracle 取前10条记录 1) select * from tbname where rownum < 11; 2) select * from (select * from tbnam ...

  6. 当前时间、前n天、后n天、取前n条记录、从第n条开始取m条

    当前时间:NOW() 前n天:DATE_SUB(NOW(),INTERVAL n DAY) 后n天:DATE_SUB(NOW(),INTERVAL -n DAY) 取前n条记录:SELECT * FR ...

  7. Oracle 取前几条记录

    今天看了篇文章,对oracle取前几条数据的方式和说明,总结比较全,学习了,做个记录点.oracle 取前10条记录 以下内容是原始文章内容,用于做留存阅读. 1.oracle 取前10条记录 1) ...

  8. oracle和sql server中,取前10条数据语法的区别

    在sql server中,取数据中前10条语句,我们可以用top 10 这样语句,但是oracle就没有这个函数,接下来介绍它们之间的区别 1.sql server 取前10语句和随机10条的语法 - ...

  9. mysql使用GROUP BY分组实现取前N条记录的方法

    MySQL中GROUP BY分组取前N条记录实现 mysql分组,取记录 GROUP BY之后如何取每组的前两位下面我来讲述mysql中GROUP BY分组取前N条记录实现方法. 这是测试表(也不知道 ...

随机推荐

  1. MySQL的几个重要配置参数详解

    .配置通用查询日志,需要在配置文件my.cnf中增加如下: () 在mysql的安装目录下,修改my.cnf配置文件,增加general_log = () 重启mysql,可执行命令/etc/init ...

  2. Java根据当前日期获得昨天的当前日期代码实现

    代码: import java.text.SimpleDateFormat; import java.util.Date; /** * 根据当前日期获得昨天的当前日期 * @author jinghu ...

  3. background--详解(背景图片根据屏幕的自适应)

    background:有以下几种属性: background-color background-position background-size background-repeat backgroun ...

  4. Tensorflow学习笔记----基础(3)

    目录: 一.TensorFlow的系统架构 二.TensorFlow的设计理念 三.TensorFlow的运行流程 四.TensorFlow的编程模型:边.节点.图.设备.变量.变量初始化.内核 五. ...

  5. python 用法测试

    Python 3.5.6 1.js风格的回调测试 def b(): ') def a(fn): if callable(fn): fn() a(b) class Sample: def q(self) ...

  6. codevs 3945 完美拓印 (KMP)

    题目大意:给你一个神奇的印章,他左右下三个面都是直的,上面是凸凹不平的面(凸凹都平行于别的面).然后给你一个轮廓线,如果一个面能与轮廓线完全重合,可以把印章的这个沿着轮廓线拓印,求所有的拓印方案. 把 ...

  7. NOIP2016 天天爱跑步 (树上差分+dfs)

    题目大意:给你一颗树,树上每个点都有一个观察员,他们仅会在 w[i] 时刻出现,观察正在跑步的玩家 一共有m个玩家,他们分别从节点 s[i] 同时出发,以每秒跑一条边的速度,沿着到 t[i] 的唯一路 ...

  8. nfs+inotify

    服务器先安装nfs服务,因为nfs服务端没有固定端口给客户端访问,所以需要借助rpc服务的111端口给客户端连接,即客户端访问rpc会调用nfs服务 yum -y install rpcbind nf ...

  9. VUE:模板语法(小白自学)

    VUE:模板语法 一:何为声明式 安装规定的语法,去实现一些效果(不需要管流程). 二:模板语法 <!DOCTYPE html> <html> <head> < ...

  10. 前端和后台对接时对sign加密方法

    前端和后台对接时对sign加密方法 /*后台php对接进行sign标签加密 1 获取向后台请求的数据data(key/value方式),可以是个对象(obj),也可以是数组(arr); 2 将数据的k ...