DataSetHelper——操作DataSet的工具类

微软知识库里面有个 DataSetHelper ,可以对DataSet中的DataTable进行Distinct、Group by、Join和Create。 我进行了整理,并添加了一些小的特性,代码如下:
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.Data;
None.gif
None.gif
namespace  Common
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// DataSet助手
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class DataSetHelper
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private class FieldInfo
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public string RelationName;
InBlock.gif            
public string FieldName;
InBlock.gif            
public string FieldAlias;
InBlock.gif            
public string Aggregate;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private DataSet ds;
InBlock.gif        
private ArrayList m_FieldInfo;
InBlock.gif        
private string m_FieldList;
InBlock.gif        
private ArrayList GroupByFieldInfo;
InBlock.gif        
private string GroupByFieldList;
InBlock.gif
InBlock.gif        
public DataSet DataSet
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn ds; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Construction#region Construction
InBlock.gif
InBlock.gif        
public DataSetHelper()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ds 
= null;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public DataSetHelper(ref DataSet dataSet)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ds 
= dataSet;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Private Methods#region Private Methods
InBlock.gif
InBlock.gif        
private bool ColumnEqual(object objectA, object objectB)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( objectA == DBNull.Value && objectB == DBNull.Value )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if ( objectA == DBNull.Value || objectB == DBNull.Value )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return ( objectA.Equals( objectB ) );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private bool RowEqual(DataRow rowA, DataRow rowB, DataColumnCollection columns)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool result = true;
InBlock.gif            
for ( int i = 0; i < columns.Count; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                result 
&= ColumnEqual( rowA[ columns[ i ].ColumnName ], rowB[ columns[ i ].ColumnName ] );
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void ParseFieldList(string fieldList, bool allowRelation)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( m_FieldList == fieldList )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif            m_FieldInfo 
= new ArrayList();
InBlock.gif            m_FieldList 
= fieldList;
InBlock.gif            FieldInfo Field;
InBlock.gif            
string[] FieldParts;
InBlock.gif            
string[] Fields = fieldList.Split( ',' );
InBlock.gif            
for ( int i = 0; i <= Fields.Length - 1; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Field 
= new FieldInfo();
InBlock.gif                FieldParts 
= Fields[ i ].Trim().Split( ' ' );
InBlock.gif                
switch ( FieldParts.Length )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
case 1:
InBlock.gif                        
//to be set at the end of the loop
InBlock.gif
                        break;
InBlock.gif                    
case 2:
InBlock.gif                        Field.FieldAlias 
= FieldParts[ 1 ];
InBlock.gif                        
break;
InBlock.gif                    
default:
InBlock.gif                        
return;
ExpandedSubBlockEnd.gif                }

InBlock.gif                FieldParts 
= FieldParts[ 0 ].Split( '.' );
InBlock.gif                
switch ( FieldParts.Length )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
case 1:
InBlock.gif                        Field.FieldName 
= FieldParts[ 0 ];
InBlock.gif                        
break;
InBlock.gif                    
case 2:
InBlock.gif                        
if ( allowRelation == false )
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
return;
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        Field.RelationName 
= FieldParts[ 0 ].Trim();
InBlock.gif                        Field.FieldName 
= FieldParts[ 1 ].Trim();
InBlock.gif                        
break;
InBlock.gif                    
default:
InBlock.gif                        
return;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if ( Field.FieldAlias == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Field.FieldAlias 
= Field.FieldName;
ExpandedSubBlockEnd.gif                }

InBlock.gif                m_FieldInfo.Add( Field );
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private DataTable CreateTable(string tableName, DataTable sourceTable, string fieldList)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataTable dt;
InBlock.gif            
if ( fieldList.Trim() == "" )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                dt 
= sourceTable.Clone();
InBlock.gif                dt.TableName 
= tableName;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                dt 
= new DataTable( tableName );
InBlock.gif                ParseFieldList( fieldList, 
false );
InBlock.gif                DataColumn dc;
InBlock.gif                
foreach ( FieldInfo Field in m_FieldInfo )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    dc 
= sourceTable.Columns[ Field.FieldName ];
InBlock.gif                    DataColumn column 
= new DataColumn();
InBlock.gif                    column.ColumnName 
= Field.FieldAlias;
InBlock.gif                    column.DataType 
= dc.DataType;
InBlock.gif                    column.MaxLength 
= dc.MaxLength;
InBlock.gif                    column.Expression 
= dc.Expression;
InBlock.gif                    dt.Columns.Add( column );
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
if ( ds != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ds.Tables.Add( dt );
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return dt;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void InsertInto(DataTable destTable, DataTable sourceTable,
InBlock.gif                                
string fieldList, string rowFilter, string sort)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ParseFieldList( fieldList, 
false );
InBlock.gif            DataRow[] rows 
= sourceTable.Select( rowFilter, sort );
InBlock.gif            DataRow destRow;
InBlock.gif            
foreach ( DataRow sourceRow in rows )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                destRow 
= destTable.NewRow();
InBlock.gif                
if ( fieldList == "" )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
foreach ( DataColumn dc in destRow.Table.Columns )
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if ( dc.Expression == "" )
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            destRow[ dc ] 
= sourceRow[ dc.ColumnName ];
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
foreach ( FieldInfo field in m_FieldInfo )
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        destRow[ field.FieldAlias ] 
= sourceRow[ field.FieldName ];
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                destTable.Rows.Add( destRow );
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void ParseGroupByFieldList(string FieldList)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( GroupByFieldList == FieldList )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif            GroupByFieldInfo 
= new ArrayList();
InBlock.gif            FieldInfo Field;
InBlock.gif            
string[] FieldParts;
InBlock.gif            
string[] Fields = FieldList.Split( ',' );
InBlock.gif            
for ( int i = 0; i <= Fields.Length - 1; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Field 
= new FieldInfo();
InBlock.gif                FieldParts 
= Fields[ i ].Trim().Split( ' ' );
InBlock.gif                
switch ( FieldParts.Length )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
case 1:
InBlock.gif                        
//to be set at the end of the loop
InBlock.gif
                        break;
InBlock.gif                    
case 2:
InBlock.gif                        Field.FieldAlias 
= FieldParts[ 1 ];
InBlock.gif                        
break;
InBlock.gif                    
default:
InBlock.gif                        
return;
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                FieldParts 
= FieldParts[ 0 ].Split( '(' );
InBlock.gif                
switch ( FieldParts.Length )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
case 1:
InBlock.gif                        Field.FieldName 
= FieldParts[ 0 ];
InBlock.gif                        
break;
InBlock.gif                    
case 2:
InBlock.gif                        Field.Aggregate 
= FieldParts[ 0 ].Trim().ToLower();
InBlock.gif                        Field.FieldName 
= FieldParts[ 1 ].Trim( ' '')' );
InBlock.gif                        
break;
InBlock.gif                    
default:
InBlock.gif                        
return;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if ( Field.FieldAlias == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if ( Field.Aggregate == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        Field.FieldAlias 
= Field.FieldName;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        Field.FieldAlias 
= Field.Aggregate + "of" + Field.FieldName;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                GroupByFieldInfo.Add( Field );
ExpandedSubBlockEnd.gif            }

InBlock.gif            GroupByFieldList 
= FieldList;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private DataTable CreateGroupByTable(string tableName, DataTable sourceTable, string fieldList)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( fieldList == null || fieldList.Length == 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return sourceTable.Clone();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DataTable dt 
= new DataTable( tableName );
InBlock.gif                ParseGroupByFieldList( fieldList );
InBlock.gif                
foreach ( FieldInfo Field in GroupByFieldInfo )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    DataColumn dc 
= sourceTable.Columns[ Field.FieldName ];
InBlock.gif                    
if ( Field.Aggregate == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        dt.Columns.Add( Field.FieldAlias, dc.DataType, dc.Expression );
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        dt.Columns.Add( Field.FieldAlias, dc.DataType );
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
if ( ds != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    ds.Tables.Add( dt );
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return dt;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void InsertGroupByInto(DataTable destTable, DataTable sourceTable, string fieldList,
InBlock.gif                                       
string rowFilter, string groupBy)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( fieldList == null || fieldList.Length == 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif            ParseGroupByFieldList( fieldList ); 
InBlock.gif            ParseFieldList( groupBy, 
false ); 
InBlock.gif            DataRow[] rows 
= sourceTable.Select( rowFilter, groupBy );
InBlock.gif            DataRow lastSourceRow 
= null, destRow = null;
InBlock.gif            
bool sameRow;
InBlock.gif            
int rowCount = 0;
InBlock.gif            
foreach ( DataRow sourceRow in rows )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sameRow 
= false;
InBlock.gif                
if ( lastSourceRow != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    sameRow 
= true;
InBlock.gif                    
foreach ( FieldInfo Field in m_FieldInfo )
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if ( !ColumnEqual( lastSourceRow[ Field.FieldName ], sourceRow[ Field.FieldName ] ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            sameRow 
= false;
InBlock.gif                            
break;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
if ( !sameRow )
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        destTable.Rows.Add( destRow );
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
if ( !sameRow )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    destRow 
= destTable.NewRow();
InBlock.gif                    rowCount 
= 0;
ExpandedSubBlockEnd.gif                }

InBlock.gif                rowCount 
+= 1;
InBlock.gif                
foreach ( FieldInfo field in GroupByFieldInfo )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
switch ( field.Aggregate.ToLower() )
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
case null
InBlock.gif                        
case "":
InBlock.gif                        
case "last":
InBlock.gif                            destRow[ field.FieldAlias ] 
= sourceRow[ field.FieldName ];
InBlock.gif                            
break;
InBlock.gif                        
case "first":
InBlock.gif                            
if ( rowCount == 1 )
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                destRow[ field.FieldAlias ] 
= sourceRow[ field.FieldName ];
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
break;
InBlock.gif                        
case "count":
InBlock.gif                            destRow[ field.FieldAlias ] 
= rowCount;
InBlock.gif                            
break;
InBlock.gif                        
case "sum":
InBlock.gif                            destRow[ field.FieldAlias ] 
= Add( destRow[ field.FieldAlias ], sourceRow[ field.FieldName ] );
InBlock.gif                            
break;
InBlock.gif                        
case "max":
InBlock.gif                            destRow[ field.FieldAlias ] 
= Max( destRow[ field.FieldAlias ], sourceRow[ field.FieldName ] );
InBlock.gif                            
break;
InBlock.gif                        
case "min":
InBlock.gif                            
if ( rowCount == 1 )
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                destRow[ field.FieldAlias ] 
= sourceRow[ field.FieldName ];
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                destRow[ field.FieldAlias ] 
= Min( destRow[ field.FieldAlias ], sourceRow[ field.FieldName ] );
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
break;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                lastSourceRow 
= sourceRow;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if ( destRow != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                destTable.Rows.Add( destRow );
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private object Min(object a, object b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( ( a is DBNull ) || ( b is DBNull ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return DBNull.Value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if ( ( (IComparable) a ).CompareTo( b ) == -1 )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return a;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return b;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private object Max(object a, object b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( a is DBNull )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return b;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if ( b is DBNull )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return a;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if ( ( (IComparable) a ).CompareTo( b ) == 1 )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return a;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return b;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private object Add(object a, object b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( a is DBNull )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return b;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if ( b is DBNull )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return a;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return ( (decimal) a + (decimal) b );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private DataTable CreateJoinTable(string tableName, DataTable sourceTable, string fieldList)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( fieldList == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return sourceTable.Clone();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DataTable dt 
= new DataTable( tableName );
InBlock.gif                ParseFieldList( fieldList, 
true );
InBlock.gif                
foreach ( FieldInfo field in m_FieldInfo )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if ( field.RelationName == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        DataColumn dc 
= sourceTable.Columns[ field.FieldName ];
InBlock.gif                        dt.Columns.Add( dc.ColumnName, dc.DataType, dc.Expression );
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        DataColumn dc 
= sourceTable.ParentRelations[ field.RelationName ].ParentTable.Columns[ field.FieldName ];
InBlock.gif                        dt.Columns.Add( dc.ColumnName, dc.DataType, dc.Expression );
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
if ( ds != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    ds.Tables.Add( dt );
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return dt;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void InsertJoinInto(DataTable destTable, DataTable sourceTable,
InBlock.gif                                    
string fieldList, string rowFilter, string sort)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( fieldList == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ParseFieldList( fieldList, 
true );
InBlock.gif                DataRow[] Rows 
= sourceTable.Select( rowFilter, sort );
InBlock.gif                
foreach ( DataRow SourceRow in Rows )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    DataRow DestRow 
= destTable.NewRow();
InBlock.gif                    
foreach ( FieldInfo Field in m_FieldInfo )
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if ( Field.RelationName == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            DestRow[ Field.FieldName ] 
= SourceRow[ Field.FieldName ];
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            DataRow ParentRow 
= SourceRow.GetParentRow( Field.RelationName );
InBlock.gif                            DestRow[ Field.FieldName ] 
= ParentRow[ Field.FieldName ];
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    destTable.Rows.Add( DestRow );
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
SelectDistinct / Distinct#region SelectDistinct / Distinct
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 按照fieldName从sourceTable中选择出不重复的行,
InBlock.gif        
/// 相当于select distinct fieldName from sourceTable
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="tableName">表名</param>
InBlock.gif        
/// <param name="sourceTable">源DataTable</param>
InBlock.gif        
/// <param name="fieldName">列名</param>
ExpandedSubBlockEnd.gif        
/// <returns>一个新的不含重复行的DataTable,列只包括fieldName指明的列</returns>

InBlock.gif        public DataTable SelectDistinct(string tableName, DataTable sourceTable, string fieldName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataTable dt 
= new DataTable( tableName );
InBlock.gif            dt.Columns.Add( fieldName, sourceTable.Columns[ fieldName ].DataType );
InBlock.gif
InBlock.gif            
object lastValue = null;
InBlock.gif            
foreach ( DataRow dr in sourceTable.Select( "", fieldName ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if ( lastValue == null || !( ColumnEqual( lastValue, dr[ fieldName ] ) ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    lastValue 
= dr[ fieldName ];
ExpandedSubBlockStart.gifContractedSubBlock.gif                    dt.Rows.Add( 
new object[]dot.gif{lastValue} );
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
if ( ds != null && !ds.Tables.Contains( tableName ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ds.Tables.Add( dt );
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return dt;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 按照fieldName从sourceTable中选择出不重复的行,
InBlock.gif        
/// 相当于select distinct fieldName1,fieldName2,dot.gif,fieldNamen from sourceTable
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="tableName">表名</param>
InBlock.gif        
/// <param name="sourceTable">源DataTable</param>
InBlock.gif        
/// <param name="fieldNames">列名数组</param>
ExpandedSubBlockEnd.gif        
/// <returns>一个新的不含重复行的DataTable,列只包括fieldNames中指明的列</returns>

InBlock.gif        public DataTable SelectDistinct(string tableName, DataTable sourceTable, string[] fieldNames)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataTable dt 
= new DataTable( tableName );
InBlock.gif            
object[] values = new object[fieldNames.Length];
InBlock.gif            
string fields = "";
InBlock.gif            
for ( int i = 0; i < fieldNames.Length; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                dt.Columns.Add( fieldNames[ i ], sourceTable.Columns[ fieldNames[ i ] ].DataType );
InBlock.gif                fields 
+= fieldNames[ i ] + ",";
ExpandedSubBlockEnd.gif            }

InBlock.gif            fields 
= fields.Remove( fields.Length - 11 );
InBlock.gif            DataRow lastRow 
= null;
InBlock.gif            
foreach ( DataRow dr in sourceTable.Select( "", fields ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if ( lastRow == null || !( RowEqual( lastRow, dr, dt.Columns ) ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    lastRow 
= dr;
InBlock.gif                    
for ( int i = 0; i < fieldNames.Length; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        values[ i ] 
= dr[ fieldNames[ i ] ];
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    dt.Rows.Add( values );
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
if ( ds != null && !ds.Tables.Contains( tableName ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ds.Tables.Add( dt );
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return dt;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 按照fieldName从sourceTable中选择出不重复的行,
InBlock.gif        
/// 并且包含sourceTable中所有的列。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="tableName">表名</param>
InBlock.gif        
/// <param name="sourceTable">源表</param>
InBlock.gif        
/// <param name="fieldName">字段</param>
ExpandedSubBlockEnd.gif        
/// <returns>一个新的不含重复行的DataTable</returns>

InBlock.gif        public DataTable Distinct(string tableName, DataTable sourceTable, string fieldName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataTable dt 
= sourceTable.Clone();
InBlock.gif            dt.TableName 
= tableName;
InBlock.gif
InBlock.gif            
object lastValue = null;
InBlock.gif            
foreach ( DataRow dr in sourceTable.Select( "", fieldName ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if ( lastValue == null || !( ColumnEqual( lastValue, dr[ fieldName ] ) ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    lastValue 
= dr[ fieldName ];
InBlock.gif                    dt.Rows.Add( dr.ItemArray );
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
if ( ds != null && !ds.Tables.Contains( tableName ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ds.Tables.Add( dt );
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return dt;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 按照fieldNames从sourceTable中选择出不重复的行,
InBlock.gif        
/// 并且包含sourceTable中所有的列。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="tableName">表名</param>
InBlock.gif        
/// <param name="sourceTable">源表</param>
InBlock.gif        
/// <param name="fieldNames">字段</param>
ExpandedSubBlockEnd.gif        
/// <returns>一个新的不含重复行的DataTable</returns>

InBlock.gif        public DataTable Distinct(string tableName, DataTable sourceTable, string[] fieldNames)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataTable dt 
= sourceTable.Clone();
InBlock.gif            dt.TableName 
= tableName;
InBlock.gif            
string fields = "";
InBlock.gif            
for ( int i = 0; i < fieldNames.Length; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                fields 
+= fieldNames[ i ] + ",";
ExpandedSubBlockEnd.gif            }

InBlock.gif            fields 
= fields.Remove( fields.Length - 11 );
InBlock.gif            DataRow lastRow 
= null;
InBlock.gif            
foreach ( DataRow dr in sourceTable.Select( "", fields ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if ( lastRow == null || !( RowEqual( lastRow, dr, dt.Columns ) ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    lastRow 
= dr;
InBlock.gif                    dt.Rows.Add( dr.ItemArray );
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
if ( ds != null && !ds.Tables.Contains( tableName ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ds.Tables.Add( dt );
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return dt;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Select Table Into#region Select Table Into
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 按sort排序,按rowFilter过滤sourceTable,
InBlock.gif        
/// 复制fieldList中指明的字段的数据到新DataTable,并返回之
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="tableName">表名</param>
InBlock.gif        
/// <param name="sourceTable">源表</param>
InBlock.gif        
/// <param name="fieldList">字段列表</param>
InBlock.gif        
/// <param name="rowFilter">过滤条件</param>
InBlock.gif        
/// <param name="sort">排序</param>
ExpandedSubBlockEnd.gif        
/// <returns>新DataTable</returns>

InBlock.gif        public DataTable SelectInto(string tableName, DataTable sourceTable,
InBlock.gif                                    
string fieldList, string rowFilter, string sort)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataTable dt 
= CreateTable( tableName, sourceTable, fieldList );
InBlock.gif            InsertInto( dt, sourceTable, fieldList, rowFilter, sort );
InBlock.gif            
return dt;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Group By Table#region Group By Table
InBlock.gif
InBlock.gif        
public DataTable SelectGroupByInto(string tableName, DataTable sourceTable, string fieldList,
InBlock.gif                                           
string rowFilter, string groupBy)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataTable dt 
= CreateGroupByTable( tableName, sourceTable, fieldList );
InBlock.gif            InsertGroupByInto( dt, sourceTable, fieldList, rowFilter, groupBy );
InBlock.gif            
return dt;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Join Tables#region Join Tables
InBlock.gif
InBlock.gif        
public DataTable SelectJoinInto(string tableName, DataTable sourceTable, string fieldList, string rowFilter, string sort)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataTable dt 
= CreateJoinTable( tableName, sourceTable, fieldList );
InBlock.gif            InsertJoinInto( dt, sourceTable, fieldList, rowFilter, sort );
InBlock.gif            
return dt;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Create Table#region Create Table
InBlock.gif
InBlock.gif        
public DataTable CreateTable(string tableName, string fieldList)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataTable dt 
= new DataTable( tableName );
InBlock.gif            DataColumn dc;
InBlock.gif            
string[] Fields = fieldList.Split( ',' );
InBlock.gif            
string[] FieldsParts;
InBlock.gif            
string Expression;
InBlock.gif            
foreach ( string Field in Fields )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                FieldsParts 
= Field.Trim().Split( " ".ToCharArray(), 3 ); // allow for spaces in the expression
InBlock.gif                
// add fieldname and datatype
InBlock.gif
                if ( FieldsParts.Length == 2 )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    dc 
= dt.Columns.Add( FieldsParts[ 0 ].Trim(), Type.GetType( "System." + FieldsParts[ 1 ].Trim(), truetrue ) );
InBlock.gif                    dc.AllowDBNull 
= true;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if ( FieldsParts.Length == 3 ) // add fieldname, datatype, and expression
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
InBlock.gif                    Expression 
= FieldsParts[ 2 ].Trim();
InBlock.gif                    
if ( Expression.ToUpper() == "REQUIRED" )
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        dc 
= dt.Columns.Add( FieldsParts[ 0 ].Trim(), Type.GetType( "System." + FieldsParts[ 1 ].Trim(), truetrue ) );
InBlock.gif                        dc.AllowDBNull 
= false;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        dc 
= dt.Columns.Add( FieldsParts[ 0 ].Trim(), Type.GetType( "System." + FieldsParts[ 1 ].Trim(), truetrue ), Expression );
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return null;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
if ( ds != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ds.Tables.Add( dt );
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return dt;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public DataTable CreateTable(string tableName, string fieldList, string keyFieldList)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataTable dt 
= CreateTable( tableName, fieldList );
InBlock.gif            
string[] KeyFields = keyFieldList.Split( ',' );
InBlock.gif            
if ( KeyFields.Length > 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DataColumn[] KeyFieldColumns 
= new DataColumn[KeyFields.Length];
InBlock.gif                
int i;
InBlock.gif                
for ( i = 1; i == KeyFields.Length - 1++i )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    KeyFieldColumns[ i ] 
= dt.Columns[ KeyFields[ i ].Trim() ];
ExpandedSubBlockEnd.gif                }

InBlock.gif                dt.PrimaryKey 
= KeyFieldColumns;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return dt;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值