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




    }



    }

  • 相关阅读:
    linux mono环境
    【百度杯】ctf夺旗大战,16万元奖励池等你拿
    【渗透技术】渗透测试技术分析_TomCat
    成功率“99%”!截止目前史上最强大电信诈骗术
    【sql注入】浅谈sql注入中的Post注入
    通过Weeman+Ettercap配合拿下路由器管理权限
    【sql注入教程】mysql注入直接getshell
    【云盘资料】Sql注入从菜鸟到高手系列教程
    【安全开发】浅谈JSP安全开发之XSS
    Python黑帽编程2.1 Python编程哲学
  • 原文地址:https://www.cnblogs.com/lypstudy/p/13366147.html
Copyright © 2011-2022 走看看