我的代码生成工具部分代码2,没办法本来发一篇文章的非要弄2次,非得出现500 Internal Server Error错误

来源:互联网 发布:股票高频数据下载 编辑:程序博客网 时间:2024/05/21 14:09

      private static string getModelContent(string tableName)
        {
            StringBuilder sb1 = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();
            StringBuilder sqlString = new StringBuilder();
            sqlString.Append("select   a.name,b.name as type  from   syscolumns    ");
            sqlString.Append("a   join   systypes     b   on   a.xtype=b.xtype   ");
            sqlString.Append("where   a.id=object_id( '"+tableName+" ') order by a.colorder ");
            DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.connString, CommandType.Text, sqlString.ToString()).Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string columnName = dt.Rows[i]["name"].ToString();
                string type = dt.Rows[i]["type"].ToString();
                cSharpTypeMappingDBType cSharpType = getcSharpTypeFromMappingTypeList(type);
                sb1.AppendLine("        private " + cSharpType.cSharpType + " _" + columnName + "=" + cSharpType .DefaultValue+ ";");
                sb2.AppendLine("        public "+cSharpType.cSharpType+" "+columnName+"");
                sb2.AppendLine("        {");
                sb2.AppendLine("            set { _"+columnName+" = value; }");
                sb2.AppendLine("            get { return _" + columnName + "; }");
                sb2.AppendLine("        }");
            
            }
            return sb1.AppendLine(sb2.ToString()).ToString();
        }
        public static string getDalCSFile(string tableName)
        {
            StringBuilder sb1 = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();
            StringBuilder sqlString = new StringBuilder();
            sqlString.Append("select   a.name,b.name as type  from   syscolumns    ");
            sqlString.Append("a   join   systypes     b   on   a.xtype=b.xtype   ");
            sqlString.Append("where   a.id=object_id( '" + tableName + " ') order by a.colorder ");
            DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.connString, CommandType.Text, sqlString.ToString()).Tables[0];
            sb1.AppendLine("using System;");
            sb1.AppendLine("using System.Collections.Generic;");
            sb1.AppendLine("using System.Text;");
            sb1.AppendLine("using System.Data;");
            sb1.AppendLine("using System.Data.SqlClient;");
            sb1.AppendLine("namespace Dal");
            sb1.AppendLine("{");
            sb1.AppendLine("    public class "+tableName);
            sb1.AppendLine("    {");         
            sb1.AppendLine(getInsertSql(tableName));
            sb1.AppendLine(getUpdateSql(tableName));
            sb1.AppendLine(getModelSql(tableName));
            sb1.AppendLine(getDeleteSql(tableName));
            sb1.AppendLine("    }");
            sb1.AppendLine("}");
            return sb1.ToString();
        }
        static    string getInsertSql(string tableName)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("        public bool Create(Model." + tableName + " model)");
            sb.AppendLine("        {");
            sb.AppendLine("            helper.Open();");
            sb.AppendLine("            StringBuilder strSql = new StringBuilder();"); 
            sb.AppendLine( getAllColumnNameFromTable(tableName, PrepareType.Insert));
            sb.AppendLine("            SqlCommand m_Command = new SqlCommand(strSql.ToString());");
            sb.AppendLine(getAllColumnNameFromTable(tableName, PrepareType.Param));
            sb.AppendLine("            bool sReturn = false;");
            sb.AppendLine("            sReturn = (int)helper.ExecuteNonQuery(m_Command) > 0 ? true : false;");
            sb.AppendLine("            helper.Close();");
            sb.AppendLine("            return sReturn;");
            sb.AppendLine("           }");
            return sb.ToString();

        }
        static string getUpdateSql(string tableName)
        {
            StringBuilder sb = new StringBuilder();         
            sb.AppendLine("        public bool Update(Model." + tableName + " model)");
            sb.AppendLine("        {");
            sb.AppendLine("            helper.Open();");
            sb.AppendLine("            StringBuilder strSql = new StringBuilder();");
           sb.AppendLine( getAllColumnNameFromTable(tableName, PrepareType.Update));
            sb.AppendLine("            SqlCommand m_Command = new SqlCommand(strSql.ToString());");
            sb.AppendLine(getAllColumnNameFromTable(tableName, PrepareType.Param));
            sb.AppendLine("            bool sReturn = false;");
            sb.AppendLine("            sReturn = (int)helper.ExecuteNonQuery(m_Command) > 0 ? true : false;");
            sb.AppendLine("            helper.Close();");
            sb.AppendLine("            return sReturn;");
            sb.AppendLine("           }");
            return sb.ToString();
        }
        //static string getListSql(string tableName)
        //{
        //}
        static string getPKTypeAndColumnNameFromTableName(string tableName)
        {
            StringBuilder sb=new StringBuilder();
            StringBuilder sb2 = new StringBuilder();
            StringBuilder sqlString = new StringBuilder();
            sqlString.Append("select   a.name,b.name as type  from   syscolumns    ");
            sqlString.Append("a   join   systypes     b   on   a.xtype=b.xtype   ");
            sqlString.Append("where   a.id=object_id( '" + tableName + " ') order by a.colorder ");
            DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.connString, CommandType.Text, sqlString.ToString()).Tables[0];
            string PK = getPK(tableName);
            string PKColumnType = string.Empty;
            bool isHasIncreaseColumn = false;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
               
                string columnName = dt.Rows[i]["name"].ToString();
                if (getIncrease(columnName, tableName))
                {
                    string type = dt.Rows[i]["type"].ToString();
                    cSharpTypeMappingDBType cSharpType = getcSharpTypeFromMappingTypeList(type);
                    sb.Append(cSharpType.cSharpType + " " + columnName);
                   
                    isHasIncreaseColumn = true;
                    return sb.ToString();
                }
                if (PK.ToLower() == columnName.ToLower())
                {
                    string type = dt.Rows[i]["type"].ToString();
                    cSharpTypeMappingDBType cSharpType = getcSharpTypeFromMappingTypeList(type);
                    sb2.Append(cSharpType.cSharpType + " " + columnName);
                }
            }

            return sb2.ToString();
        }
        public static string getModelSql(string tableName)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("        public Model." + tableName + " getModel(" + getPKTypeAndColumnNameFromTableName(tableName) + ")");
            sb.AppendLine("        {");
            sb.AppendLine("            StringBuilder strSql = new StringBuilder();");
            sb.AppendLine(getAllColumnNameFromTable(tableName, PrepareType.getModel));         
            sb.AppendLine("            helper.Open();");
            sb.AppendLine("            SqlCommand m_Command = new SqlCommand(strSql.ToString());");
            sb.AppendLine(getAllColumnNameFromTable(tableName, PrepareType.PK));
            sb.AppendLine("            using (SqlDataReader dr = helper.ExecuteReader(m_Command))");
            sb.AppendLine("            {");
            sb.AppendLine("                if (dr.HasRows)");
            sb.AppendLine("                {");
            sb.AppendLine("                    dr.Read();");
            StringBuilder sqlString = new StringBuilder();
            sqlString.Append("select   a.name,b.name as type  from   syscolumns    ");
            sqlString.Append("a   join   systypes     b   on   a.xtype=b.xtype   ");
            sqlString.Append("where   a.id=object_id( '" + tableName + " ') order by a.colorder ");
            DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.connString, CommandType.Text, sqlString.ToString()).Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string columnName = dt.Rows[i]["name"].ToString();
                string type = dt.Rows[i]["type"].ToString();
                cSharpTypeMappingDBType cSharpType = getcSharpTypeFromMappingTypeList(type);
                sb.AppendLine("                    model." + columnName + " = (" + cSharpType.cSharpType+ ")dr[/"" + columnName + "/"];");
            }
            sb.AppendLine("");
            sb.AppendLine("                }");
            sb.AppendLine("            }");
            sb.AppendLine("            helper.Close();");
            sb.AppendLine("            return model;");
            sb.AppendLine("           }");
            return sb.ToString();
        }
        public static string getDeleteSql(string tableName)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("        public bool Delete("+getPKTypeAndColumnNameFromTableName(tableName)+")");
            sb.AppendLine("        {");
            sb.AppendLine("            StringBuilder strSql = new StringBuilder();");
            sb.AppendLine(getAllColumnNameFromTable(tableName, PrepareType.Delete));
            sb.AppendLine("            helper.Open();");
            sb.AppendLine("            SqlCommand m_Command = new SqlCommand(strSql.ToString());");
            sb.AppendLine(getAllColumnNameFromTable(tableName, PrepareType.PK));

            sb.AppendLine("            bool sReturn = false;");
            sb.AppendLine("            sReturn = (int)helper.ExecuteNonQuery(m_Command) > 0 ? true : false;");
            sb.AppendLine("            helper.Close();");
            sb.AppendLine("            return sReturn;");
 
            sb.AppendLine("           }");
            return sb.ToString();
        }
        public static cSharpTypeMappingDBType getcSharpTypeFromMappingTypeList(string dbtype)
        {
            cSharpTypeMappingDBType cSharptype = new cSharpTypeMappingDBType();
            foreach (cSharpTypeMappingDBType model in list)
            {
                if (model.dbType == dbtype)
                {
                    cSharptype = model;
                }
            }
            return cSharptype;
        }

    }
    public class cSharpTypeMappingDBType
    {
        private string _cSharpType = string.Empty;
        private string _dbType = string.Empty;
        private string _defaultValue = string.Empty;
        public string cSharpType
        {
            get { return _cSharpType; }
            set { _cSharpType = value; }
        }
        public string dbType
        {
            get { return _dbType; }
            set { _dbType = value; }
        }
        public cSharpTypeMappingDBType(string cSharpType, string dbType, string defaultValue)
        {
            this.cSharpType = cSharpType;
            this.dbType = dbType;
            this.DefaultValue = defaultValue;
        }
        public cSharpTypeMappingDBType()
        {
        }
        public string DefaultValue
        {
            get { return _defaultValue; }
            set { _defaultValue = value; }
        }

    }

原创粉丝点击