zoukankan      html  css  js  c++  java
  • Model类代码生成器

    using Humanizer;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace CodeGenerate
    {
        class Program
        {
            static void Main(string[] args)
            {
                DataTable dt = ExecuteDataTable("select * from information_schema.tables");
                foreach (DataRow row in dt.Rows)
                {
                    string tablename = (string)row["TABLE_NAME"];
    
                    
                    CreateModel(tablename);
                }
    
    
            }
    
            private static string connectionString = "server=10.100.3.110,60005;User ID=sa;Password=sa123.com;database=p2p;Enlist=false";
            private static string namespaceStr= "LLY.Model.LiCai";
            private static string filePath = @"D:Models";
            static string kongge1 = "	";
            static string kongge2 = "		";
            static string kongge3 = "			";
            static string kongge4 = "				";
            StringBuilder SB = new StringBuilder();
    
            #region 把数据库类型转化为.net类型
            private static string ToNetType(string dataType)
            {
                switch (dataType)
                {
                    case "bigint":
                        return "long?";
                    case "int":
                        return "int?";
                    case "nvarchar":
                    case "varchar":
                    case "char":
                    case "nchar":
                        return "string";
                    case "bit":
                        return "bool?";
                    case "datetime":
                        return "DateTime?";
                    default:
                        return "object";
                }
            }
            #endregion
    
            #region 数据库连接操作
            public static DataTable ExecuteDataTable(string cmdText, params SqlParameter[] parameters)
            //不能写成static
            {
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    //WhetherCon(txtConnSr.Text);//待优化   
                    /************此处写等待用户输入的代码********************/
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = cmdText;
                        cmd.Parameters.AddRange(parameters);
                        DataTable dt = new DataTable();
                        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                        adapter.Fill(dt);
                        return dt;
                    }
                }
            }
            #endregion
    
          
    
           
            #region 生成Model
            /// <summary>
            /// 生成Model
            /// </summary>
            /// <param name="tablename"></param>
            private static void CreateModel(string tablename)
            {
                string className = tablename.Singularize();
                DataTable dtCols = ExecuteDataTable("select * from information_schema.columns where table_name=@tablename", new SqlParameter("tablename", tablename));//得到选中的表
    
                DataTable dtColsDesc = ExecuteDataTable("SELECT major_id, minor_id, c.name as Column_Name,  value AS extendedProperty "
    + "FROM sys.extended_properties AS ep "
    +"INNER JOIN sys.tables AS t ON ep.major_id = t.object_id "
    +"INNER JOIN sys.columns AS c ON ep.major_id = c.object_id AND ep.minor_id = c.column_id "
    + "WHERE class = 1 and t.name =@tablename", new SqlParameter("tablename", tablename));//得到选中的表
    
                StringBuilder sb = new StringBuilder();//用来拼接字符串的对象
                sb.AppendLine("using System;");
                sb.AppendLine("using System.Collections.Generic;");
                sb.AppendLine("using System.Linq;");
                sb.AppendLine("using System.Text;");
                sb.AppendLine("using LLY.Core.LiCai.Data;");
                sb.AppendLine("using System.Threading.Tasks;
    ");
                sb.AppendLine("namespace " + namespaceStr);
                sb.AppendLine("{
    
    ");
                sb.AppendLine(kongge1 + $"[Serializable]");
                sb.AppendLine(kongge1 + $"public class {className} : BaseEntity");
                sb.AppendLine(kongge1 + "{
    ");
                foreach (DataRow row in dtCols.Rows)
                /*★参数中数据库类型和.net的数据类型之间的转换*/
                //遍历每行,得到要用的参数,并赋给其它变量
                {
                    string colName = (string)row["Column_Name"];
                    string dataType = (string)row["Data_Type"];
                    string netType = ToNetType(dataType);
    
                    if(colName=="Id"|| colName == "CreatedAt" || colName == "LastModifiedAt")
                    {
                        continue;
                    }
    
                    string propDesc = string.Empty;
                    foreach (DataRow item in dtColsDesc.Rows)
                    {
                        string colName1 = (string)item["Column_Name"];
                        string desc = (string)item["extendedProperty"];
                        if (colName1 == colName)
                        {
                            propDesc = desc;
                            break;
                        }
                    }
    
    
                    sb.AppendLine(kongge2 + "/// <summary>");
                    sb.AppendLine(kongge2 + $"/// {propDesc}");
                    sb.AppendLine(kongge2 + "/// <summary>");
                    sb.AppendLine(kongge2 + "public" + " " + netType + " " + colName + "{ get; set; }
    ");
                    
                }
                sb.AppendLine(kongge1 + "}");
    
    
    
    
                sb.AppendLine("
    
    ");
                sb.AppendLine(kongge1 + $"public class {className}Query : BaseQuery<{className}>");
                sb.AppendLine(kongge1 + "{");
                sb.AppendLine("
    
    ");
                sb.AppendLine(kongge1 + "}");
    
                sb.AppendLine("}");
                File.WriteAllText(filePath + @"" + className + ".cs", sb.ToString());
            }
            #endregion
    
    
            
            
        }
    
    
        
    }
  • 相关阅读:
    C# 字段 属性 方法 三霸主齐上阵
    C# 项目之总结,浓缩才是精华
    C# 语法 重中之重
    岁月流逝,C#成为我们IT精英永恒不变的经典
    pv:快餐厅4类职员正确并发运行的程序——用pv实现有向图的前驱关系哦!
    pv错题:用pv操作实现旅客,汽车之间的同步关系——北航2018考研计算机!!!!!!!!我还很不熟练,奋斗吧少年!!!!!!
    windows 的 DMA
    pv打卡:生产者消费者问题扩展——>南航2002pv习题哦
    冷门的OS知识:中断——这个我多大熟练qwq!
    Unix混合索引结构_文件系统_文件的组织_文件的物理结构_多级索引文件——>相关的小计算题!!!!!!重点重点重点!!!!!!
  • 原文地址:https://www.cnblogs.com/yxlblogs/p/10071998.html
Copyright © 2011-2022 走看看