zoukankan      html  css  js  c++  java
  • C# model代码生成器

    using System.Collections.Generic;
    using System.Text;
    
        public class Class1
        {
            //传递 1.表名 2.列名 3.类型
            public void GenerateModel(string TableName, string ColumnName, string TypeName) //生成Model
            {
                #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("DictID", 56);
                DicColum.Add("DictType", 231);
                DicColum.Add("ParentID", 56);
                DicColum.Add("Subtitle", 231);
                DicColum.Add("Sorting", 56);
                #endregion
    
                TypeName = "Model";
                TableName = "TableName";
                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" + "
    "); //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(";
    ");
                        }
                    }
                }
    
                foreach (var i in DicColum)
                {
                    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("{
    "); //set{ _dicttype=value;}
                            sb.AppendFormat("set{{ _{0} =value;}}", i.Key.ToLower());
                            sb.AppendFormat("get{{return _{0} ;}}", i.Key.ToLower());
                            sb.Append("
    }");                   
                        }
                    }
                }
    
                #endregion
                sb.Append("
    " + "#endregion"); //End
    
                sb.Append("
    " + "}}");
                //sb.AppendFormat("using System;namespace {0}{ public partial class Dictionary{1}{ public {1}(){}}}");
                string ss = sb.ToString();
            }
    
            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}
    {
    ", TypeName);
                sb.AppendFormat(" public partial class {0}
    {
    ", TableName);
                sb.AppendFormat("public {0}(){}", TableName); //构造函数
                sb.AppendFormat("public int Add({0} model)
    {{
    ", "Model模型名称");
                sb.Append("StringBuilder strSql = new StringBuilder();");
                //这种方法不好---------终止
    
            }
        }
  • 相关阅读:
    功能强大表格控件Spread for Windows Forms 中文版发布
    charles 证书在 andriod 7 及更高版本手机上的安装
    Hibernate 开发中问题
    Silverlight中DataForm对数据进行校验
    LightSpeed ORM .NET简单运用
    Hibernate manytomany实现
    C# _lopen判断文件是否正在被使用
    SQL Join
    Castle与Mixin
    Catalysis 的构成部分与框架
  • 原文地址:https://www.cnblogs.com/enych/p/8808738.html
Copyright © 2011-2022 走看看