www.pudn.com > WorkflowDesigner.rar > DO_GennCode.cs, change:2008-03-20,size:4979b


using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Data; 
using DataAccess; 
using System.Collections; 
namespace Bussiness 
{ 
    public class DO_GennCode 
    { 
        public DO_GennCode() 
        { } 
 
 
        public DataTable GetSturctByTableName(String TableName) 
        { 
            String Sql = "Select * from " + TableName + " where 1=-1"; 
            IDataAccess da = DataAccessFactory.CreateDataAccess(); 
            DataSet ds = da.ExecuteDataset(Sql); 
            if (ds.Tables == null) 
                return null; 
            if (ds.Tables.Count <= 0) 
                return null; 
            return ds.Tables[0]; 
        } 
 
        public String GennCode(String TableName) 
        { 
            StringBuilder sb = new StringBuilder(); 
            DataTable dt = this.GetSturctByTableName(TableName); 
            if (dt != null) 
            { 
                String FunctionParam=this.GetFunctionParam(dt); 
                String strFunction = String.Format("private int Add"+TableName+"Info({0},IDataAccess da)", FunctionParam); 
                sb.AppendLine(strFunction); 
                sb.AppendLine("        {"); 
                sb.AppendLine("            int " + TableName + "Id = this.GetMaxId(\"" + TableName + "\", da);"); 
                String SqlContent = String.Format("            String sql = \"insert into " + TableName + "({0})values({1})\";", GetColoum(dt), GetValueColoum(dt)); 
                sb.AppendLine(SqlContent); 
 
 
                sb.AppendLine("            QueryParameterCollection qpc = new QueryParameterCollection();"); 
 
                int i = 0; 
                foreach (DataColumn dc in dt.Columns) 
                { 
                    String DBTYPE = dc.DataType.ToString().Replace("System.",""); 
                    sb.AppendLine("            QueryParameter qp" + i.ToString() + " = new QueryParameter(\"" + dc.ColumnName + "\", DbType." + DBTYPE + ");"); 
                    if (dc.ColumnName == "ID") 
                    { 
                        sb.AppendLine("            qp" + i.ToString() + ".Value = Convert.ToDecimal("+TableName + "Id);"); 
                    } 
                    else 
                    { 
                        sb.AppendLine("            if (" + dc.ColumnName + " == \"\")"); 
                        sb.AppendLine("            {"); 
                        sb.AppendLine("                qp" + i.ToString() + ".Value = DBNull.Value;"); 
                        sb.AppendLine("            }"); 
                        sb.AppendLine("            else"); 
                        sb.AppendLine("            {"); 
                        sb.AppendLine("                qp" + i.ToString() + ".Value = Convert.To" + DBTYPE + "(" + dc.ColumnName + "); "); 
                        sb.AppendLine("            }"); 
 
                    } 
                    //sb.AppendLine("            qp" + i.ToString() + ".Value = Convert.To" + DBTYPE + "(SetValue(" + dc.ColumnName + ")); "); 
                    sb.AppendLine("            qpc.Add(qp" + i.ToString() + ");"); 
                     
                    i++; 
 
                } 
 
                sb.AppendLine("            da.ExecuteNonQuery(sql, qpc);"); 
                sb.AppendLine("            return " + TableName + "Id;"); 
                sb.AppendLine("        }"); 
             
              
            } 
 
 
 
            return sb.ToString(); 
        } 
 
        private String GetFunctionParam(DataTable dt) 
        { 
            String Reslut=""; 
            ArrayList al = new ArrayList(); 
            foreach (DataColumn dc in dt.Columns) 
            { 
                al.Add("String "+dc.ColumnName); 
            } 
            if (al.Count > 0) 
            { 
                String[] temp =(String[])al.ToArray(typeof(String)); 
                Reslut = String.Join(",", temp); 
            } 
            return Reslut; 
        } 
 
        private String GetColoum(DataTable dt) 
        { 
            String Reslut = ""; 
            ArrayList al = new ArrayList(); 
            foreach (DataColumn dc in dt.Columns) 
            { 
                al.Add( dc.ColumnName); 
            } 
            if (al.Count > 0) 
            { 
                String[] temp = (String[])al.ToArray(typeof(String)); 
                Reslut = String.Join(",", temp); 
            } 
            return Reslut; 
        } 
 
        private String GetValueColoum(DataTable dt) 
        { 
            String Reslut = ""; 
            ArrayList al = new ArrayList(); 
            foreach (DataColumn dc in dt.Columns) 
            { 
                al.Add(":" + dc.ColumnName); 
            } 
            if (al.Count > 0) 
            { 
                String[] temp = (String[])al.ToArray(typeof(String)); 
                Reslut = String.Join(",", temp); 
            } 
            return Reslut; 
        } 
 
         
    } 
}