1using System; 
  2using System.Collections; 
  3using System.Data; 
  4 
  5namespace Common 
  6
  7    /**//// <summary> 
  8    /// DataSet助手 
  9    /// </summary> 

 10    public class DataSetHelper 
 11    
 12        private class FieldInfo 
 13        
 14            public string RelationName; 
 15            public string FieldName; 
 16            public string FieldAlias; 
 17            public string Aggregate; 
 18        }
 
 19 
 20        private DataSet ds; 
 21        private ArrayList m_FieldInfo; 
 22        private string m_FieldList; 
 23        private ArrayList GroupByFieldInfo; 
 24        private string GroupByFieldList; 
 25 
 26        public DataSet DataSet 
 27        
 28            get return ds; } 
 29        }
 
 30 
 31        构造方法 
 44 
 45        私有方法 
505 
506        SelectDistinct / Distinct 
640 
641        Select Table Into 
662 
663        #region Group By Table 
664 
665        public DataTable SelectGroupByInto(string tableName, DataTable sourceTable, string fieldList, 
666            string rowFilter, string groupBy) 
667        
668            DataTable dt = CreateGroupByTable( tableName, sourceTable, fieldList ); 
669            InsertGroupByInto( dt, sourceTable, fieldList, rowFilter, groupBy ); 
670            return dt; 
671        }
 
672 
673        #endregion
 
674 
675        Join Tables 
685 
686        #region Create Table 
687 
688        public DataTable CreateTable(string tableName, string fieldList) 
689        
690            DataTable dt = new DataTable( tableName ); 
691            DataColumn dc; 
692            string[] Fields = fieldList.Split( ',' ); 
693            string[] FieldsParts; 
694            string Expression; 
695            foreach ( string Field in Fields ) 
696            
697                FieldsParts = Field.Trim().Split( " ".ToCharArray(), 3 ); // allow for spaces in the expression 
698                // add fieldname and datatype 
699                if ( FieldsParts.Length == 2 ) 
700                
701                    dc = dt.Columns.Add( FieldsParts[ 0 ].Trim(), Type.GetType( "System." + FieldsParts[ 1 ].Trim(), truetrue ) ); 
702                    dc.AllowDBNull = true
703                }
 
704                else if ( FieldsParts.Length == 3 ) // add fieldname, datatype, and expression 
705                
706                    Expression = FieldsParts[ 2 ].Trim(); 
707                    if ( Expression.ToUpper() == "REQUIRED" ) 
708                    
709                        dc = dt.Columns.Add( FieldsParts[ 0 ].Trim(), Type.GetType( "System." + FieldsParts[ 1 ].Trim(), truetrue ) ); 
710                        dc.AllowDBNull = false
711                    }
 
712                    else 
713                    
714                        dc = dt.Columns.Add( FieldsParts[ 0 ].Trim(), Type.GetType( "System." + FieldsParts[ 1 ].Trim(), truetrue ), Expression ); 
715                    }
 
716                }
 
717                else 
718                
719                    return null
720                }
 
721            }
 
722            if ( ds != null ) 
723            
724                ds.Tables.Add( dt ); 
725            }
 
726            return dt; 
727        }
 
728 
729        public DataTable CreateTable(string tableName, string fieldList, string keyFieldList) 
730        
731            DataTable dt = CreateTable( tableName, fieldList ); 
732            string[] KeyFields = keyFieldList.Split( ',' ); 
733            if ( KeyFields.Length > 0 ) 
734            
735                DataColumn[] KeyFieldColumns = new DataColumn[KeyFields.Length]; 
736                int i; 
737                for ( i = 1; i == KeyFields.Length - 1++i ) 
738                
739                    KeyFieldColumns[ i ] = dt.Columns[ KeyFields[ i ].Trim() ]; 
740                }
 
741                dt.PrimaryKey = KeyFieldColumns; 
742            }
 
743            return dt; 
744        }
 
745 
746        #endregion
 
747    }

748}

749    
posted on 2008-10-25 20:59  Dot-Boy  阅读(610)  评论(0编辑  收藏  举报