写代码生成器
今天突然想写个代码生成器。
写一个代码生成器最主要的是,①需要获取库下面所有的表 ②获取表中所有的字段,注释,类型 ③前2者具备就可以写个代码生成器
一:先上个简单的界面
二:操作数据方法
public class GetDataTest { /// <summary> /// 操作数据库方法 /// </summary> /// <param name="sql">SQL语句</param> /// <returns></returns> public static MySqlDataReader GetData(string sql) { try { var connection = ConfigurationManager.ConnectionStrings["mysql"].ConnectionString; MySqlConnection con = new MySqlConnection(connection); if (con.State == ConnectionState.Closed) con.Open(); using (MySqlCommand cmd = new MySqlCommand(sql, con)) { con.Open(); cmd.CommandType = CommandType.Text; return cmd.ExecuteReader(CommandBehavior.CloseConnection); } } catch (Exception) { return null; } } }
三:获取所有表
var sql = "select TABLE_NAME from information_schema.tables where TABLE_SCHEMA='库名'"; using (var read = GetDataTest.GetData(sql)) { if (read.HasRows) { while (read.Read()) {
//循环获取表明 object k = read.GetValue(0); } } }
四:获取表中的字段,注释,类型
var sql = "select COLUMN_NAME,column_comment, DATA_TYPE from information_schema.COLUMNS where table_name ='表名'"; using (var read = GetDataTest.GetData(sql)) { if (read.HasRows) { while (read.Read()) { //根据sql查询字段的顺序排列,COLUMN_NAME,column_comment, DATA_TYPE //字段名 var field = read.GetValue(0); //字段注释 var comment = read.GetValue(1); //字段类型 var type = read.GetValue(2).ToString(); } } }
拼接model类的例子
/// <summary> /// 生成代码的方法 /// </summary> /// <param name="projectName">项目名称</param> /// <param name="tableName">表名称</param> /// <param name="arg">参数</param> public override object Make(string projectName, string tableName, dynamic arg) { try {
//拼接Model类 StringBuilder sbm = new StringBuilder(); sbm.AppendLine("using System;"); sbm.AppendLine("using System.Text;"); sbm.AppendLine($"namespace {projectName}.Model"); sbm.AppendLine("{"); sbm.AppendLine($"public class {tableName}Model"); sbm.AppendLine("{");
var reader = (MySqlDataReader)arg; while (reader.Read()) { //一个字段有个属性 var count = reader.FieldCount; if (count <= 0) continue; //字段名 var field = reader.GetValue(0); //字段注释 var comment = reader.GetValue(1); //字段类型 var type = reader.GetValue(2).ToString(); sbm.AppendLine("/// <summary>"); sbm.AppendLine($"/// {comment}"); sbm.AppendLine("/// <summary>"); sbm.AppendLine($"public {type.Switch()} {field}" + "{ get; set; }"); } sbm.AppendLine("}"); sbm.AppendLine("}"); return sbm; } catch (Exception) { return null; } }
//1.生成model数据 MakeCode mm = new MakeModel(); var content = mm.Make(projectName, tableName, item); var path = AppDomain.CurrentDomain.BaseDirectory + "../../CodeFile/"; var modelPath = path + $"Model/{tableName}Model.cs"; mf.Create(modelPath, null, content);
生成文件
public class MakeFile : CreateFile { /// <summary> /// 创建文件 /// </summary> /// <param name="path">路径</param> /// <param name="tableName">表名</param> /// <param name="content">生成内容</param> public override void Create(string path, string tableName, object content) { try { //如果存在文件就删除 if (File.Exists(path)) { File.Delete(path); } File.Create(path).Close(); StringBuilder sb = (StringBuilder)content; File.WriteAllText(path, sb.ToString()); sb?.Clear(); } catch (Exception ex) { throw; } } }
分享链接 密码:uhzr