C# 生成Model和DAL

using Model;
using System.Collections.Generic;
using System.Text;

public class Class1
{

    #region 生成Model類
    public void testff()
    {
        #region  數據庫ID所對應的類型值
        Dictionary<int, string> DicType = new Dictionary<int, string>();
        DicType.Add(56, "int");
        DicType.Add(231, "string");
        #endregion
        #region 列名集合和數據ID
        Dictionary<string, int> DicColum = new Dictionary<string, int>();
        DicColum.Add("name", 231);//名字
        DicColum.Add("column_id", 56);//列ID
        DicColum.Add("system_type_id", 56); //系統類型
        DicColum.Add("user_type_id", 56);//用戶類型
        DicColum.Add("max_length", 56); //最大長度
        DicColum.Add("is_nullable", 56); //是否可空
        #endregion

        GenerateModel(DicType, DicColum);
    }//傳遞參數GenerateModel(DIC,DIC);
    public void GenerateModel(Dictionary<int, string> DicType, Dictionary<string, int> DicColum) //生成Model類字符串
    {

        string TypeName = "Model";
        string TableName = "ColumnInfo"; //列名信息
        TableName = TableName + TypeName;

        StringBuilder sb = new StringBuilder();
        sb.Append("using System;"); //引入命名空間
        sb.Append("namespace "); //命名空間名字
        sb.Append(TypeName + "{");
        sb.Append("    public partial class ");
        sb.Append(TableName + "{");
        sb.Append("public " + TableName + "(){}");


        sb.Append("#region Model" + "\r\n"); //Begin
        #region //內容
        foreach (var i in DicColum) //增長字段
        {
            foreach (var j in DicType)
            {
                if (i.Value == j.Key) // DicColum的56== DicType的56
                {
                    sb.Append("private ");
                    sb.Append(j.Value);
                    sb.Append(" _");
                    //sb.Append(i.Key); //DicColumd的DictID   //轉換一下大小寫在追加
                    sb.Append(i.Key.ToLower());
                    sb.Append(";\r\n");
                }
            }
        }

        foreach (var i in DicColum)//增長get/set
        {
            foreach (var j in DicType)
            {
                if (i.Value == j.Key) // DicColum的56== DicType的56
                {
                    sb.Append("public ");
                    sb.Append(j.Value);
                    sb.Append(" ");
                    sb.Append(i.Key);
                    sb.Append("{\r\n"); //set{ _dicttype=value;}
                    sb.AppendFormat("set{{ _{0} =value;}}", i.Key.ToLower());
                    sb.AppendFormat("get{{return _{0} ;}}", i.Key.ToLower());
                    sb.Append("\r\n}");
                }
            }
        }

        #endregion
        sb.Append("\r\n" + "#endregion"); //End

        sb.Append("\r\n" + "}}");
        //sb.AppendFormat("using System;namespace {0}{ public partial class Dictionary{1}{ public {1}(){}}}");
        string ss = sb.ToString();
    }
    public void GenerateModel(List<ColumnInfoModel> list)
    {
        #region  數據庫ID所對應的類型值
        Dictionary<int, string> DicType = new Dictionary<int, string>();
        DicType.Add(56, "int");
        DicType.Add(231, "string");
        #endregion

        string ClassName = "類名";
        StringBuilder sb = new StringBuilder();
        sb.Append("using System;"); //引入命名空間
        sb.AppendFormat("namespace {0}\r\n{{\r\n", "Model");
        sb.AppendFormat("public partial class {0}\r\n{{\r\n", ClassName);
        sb.AppendFormat("public {0}(){{}}", ClassName); //構造函數

        sb.Append("#region Model" + "\r\n"); //Begin
        #region 內容
        foreach (var i in list)
        {
            foreach (var j in DicType)
            {
                if (i.system_type_id == j.Key)
                {
                    sb.Append("private ");
                    sb.Append(j.Value);
                    sb.Append(" _");
                    sb.Append(i.name.ToLower());
                    sb.Append(";\r\n");
                }
            }
        }
        foreach (var i in list)
        {
            foreach (var j in DicType)
            {
                if (i.system_type_id == j.Key)
                {
                    sb.Append("public ");
                    sb.Append(j.Value);
                    sb.Append(" ");
                    sb.Append(i.name);
                    sb.Append("{\r\n"); //set{ _dicttype=value;}
                    sb.AppendFormat("set{{ _{0} =value;}}", i.name.ToLower());
                    sb.AppendFormat("get{{return _{0} ;}}", i.name.ToLower());
                    sb.Append("\r\n}");
                }
            }
        }
        #endregion
        sb.Append("\r\n" + "#endregion"); //End
        sb.Append("\r\n" + "}}");
        string ss = sb.ToString();
    } //生成model類 字符串
    #endregion

    #region 生成DAL類

    public void GenerateDAL(string TableName, string ColumnName, string TypeName)
    {
        TypeName = "DAL";
        TableName = "ClassName";
        TableName = TableName + TypeName;

        StringBuilder sb = new StringBuilder();
        sb.Append("using System;"); //引入命名空間
        sb.Append("using System.Data;");
        sb.Append("using System.Text;");
        sb.Append("using System.Data.SqlClient;");
        sb.Append("using System.Collections.Generic;");
        sb.AppendFormat("namespace {0}\r\n{\r\n", TypeName);
        sb.AppendFormat("public partial class {0}\r\n{\r\n", TableName);
        sb.AppendFormat("public {0}(){}", TableName); //構造函數
        sb.Append("#region  --基本功能"); //基本功能
        //--添加功能
        sb.AppendFormat("public int Add({0} model)", TableName);
        sb.Append("\r\n{\r\n");
        sb.Append("StringBuilder strSql = new StringBuilder();");

        sb.AppendFormat("strSql.Append(\"insert into {0}(\");", TableName.Replace(TypeName, ""));
        sb.AppendFormat("{0}) values (", "DictType,ParentID,Subtitle,Sorting"); //列名

        sb.AppendFormat("{0})", "@DictType,@ParentID,@Subtitle,@Sorting");
        sb.Append(";select @@IDENTITY");
        sb.Append("SqlParameter[] parameters = {\r\n");




        sb.Append("\r\n}\r\n");
        sb.Append("#endregion");//end;

        sb.AppendFormat("public int Add({0} model)\r\n{{\r\n", "Model模型名稱");
        sb.Append("StringBuilder strSql = new StringBuilder();");


    }
    public void GenerateDAL(List<ColumnInfoModel> list)
    {
        #region  數據庫ID所對應的類型值
        Dictionary<int, string> DicType = new Dictionary<int, string>();
        DicType.Add(56, "int");
        DicType.Add(231, "string");
        #endregion

        string ClassName = "Dictionary";//類名
        string ClassNameDAL = "Dictionary" + "DAL";
        string ClassNameModel = "Dictionary" + "Model";
        StringBuilder sb = new StringBuilder();
        sb.Append("using System;"); //引入命名空間
        sb.Append("using System.Data;");
        sb.Append("using System.Text;");
        sb.Append("using System.Data.SqlClient;");
        sb.Append("using Model;");
        sb.AppendFormat("namespace {0}\r\n{{\r\n", "DAL");
        sb.AppendFormat("public partial class {0}\r\n{{\r\n", ClassNameDAL);
        sb.AppendFormat("public {0}(){{}}\r\n", ClassNameDAL); //構造函數

        sb.Append("#region  --基本功能\r\n"); //基本功能
        #region //增長一個Model
        //增長一個Model
        sb.AppendFormat("public int Add({0} model)", ClassNameModel);
        sb.Append("\r\n{\r\n");
        sb.Append("StringBuilder strSql = new StringBuilder();");
        sb.Append("\r\n");
        sb.AppendFormat("strSql.Append(\"insert into {0}(\");", ClassName);
        sb.Append("\r\n");
        sb.AppendFormat("strSql.Append(\"{0})\");", "DictType,ParentID,Subtitle,Sorting");//列名字符串
        sb.Append("\r\n");
        sb.Append("strSql.Append(\" values(\");");
        sb.Append("\r\n");
        sb.AppendFormat("strSql.Append(\"{0})\");", "@DictType,@ParentID,@Subtitle,@Sorting)");//@列名字符串
        sb.Append("\r\n");
        sb.Append("strSql.Append(\"; select @@IDENTITY\");");
        sb.Append("\r\n");
        sb.Append("SqlParameter[] parameters = {\r\n");
        //// //new SqlParameter("@DictType", SqlDbType.NVarChar,50),
        foreach (var i in list)
        {
            foreach (var j in DicType)
            {
                if (i.system_type_id == j.Key)//DicColum的56== DicType的56
                {
                    if (j.Value == "int")
                    {
                        sb.Append("\t\t\t");
                        sb.AppendFormat("new SqlParameter(\"@{0}\", SqlDbType.{1},{2}),", i.name, "Int", i.max_length);
                        sb.Append("\r\n");
                    }
                    if (j.Value == "string")
                    {
                        sb.Append("\t\t\t");
                        sb.AppendFormat("new SqlParameter(\"@{0}\", SqlDbType.{1},{2}),", i.name, "NVarChar", i.max_length / 2);
                        sb.Append("\r\n");
                    }
                }
            }
        }
        sb.Remove(sb.Length - 3, 3);
        sb.Append("\r\n\t\t\t};\r\n");// SqlParameter[] parameters = {};
        foreach (var i in list)
        {
            sb.AppendFormat("parameters[{0}].Value = model.{1};", list.IndexOf(i), i.name);
            sb.Append("\r\n");
        }
        sb.Append("object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);");
        sb.Append("\r\n");
        sb.Append("if (obj == null) \r\n{\r\n return 0; \r\n}\r\n else \r\n{\r\n  return Convert.ToInt32(obj); \r\n}\r\n");
        sb.Append("\r\n}\r\n");
        //--end
        #endregion

        #region  更新一條數據 
        //---終止 這種不靈活。。。。。

        #endregion

        sb.Append("#endregion");//end;
        sb.Append("\r\n}\r\n");
        sb.Append("\r\n}\r\n");
        string ss = sb.ToString();
    }
   #endregion
}
相關文章
相關標籤/搜索