自己写代码生成器之生成Model层(获取数据库所有表名称)

来源:互联网 发布:蚁群算法基本流程图 编辑:程序博客网 时间:2024/05/08 20:13

--得到数据库birthday所有表名称
select name from sysobjects where [type]='U'
--select [TABLE_NAME] from  INFORMATION_SCHEMA.TABLES where [TABLE_TYPE]='BASE TABLE'


--获取列信息,不获取数据
select top 0 * from userInfo


思路:拼接字符串


public Index()        {            InitializeComponent();        }        /// <summary>        /// 加载        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void Index_Load(object sender, EventArgs e)        {            string sql = "select [TABLE_NAME] from  INFORMATION_SCHEMA.TABLES where [TABLE_TYPE]='BASE TABLE'";            DataTable data = SqlHelper.ExeccutDataTable(sql);            if (data.Rows.Count > 0)            {                this.cmb_tableName.Items.Clear();                foreach (DataRow row in data.Rows)                {                    string name = row["TABLE_NAME"].ToString();                    this.cmb_tableName.Items.Add(name);                }                this.cmb_tableName.SelectedIndex = 0;            }        }        /// <summary>        /// 生成代码        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btn_generate_Click(object sender, EventArgs e)        {            string tableName = this.cmb_tableName.SelectedItem.ToString();            string sql = "select top 0 * from " + tableName;            DataTable data = SqlHelper.ExeccutDataTable(sql);            ///生成model层代码            GenerateModelCode(tableName, data);        }        #region Model        /// <summary>        /// 生成model层代码        /// </summary>        /// <param name="tableName">表名称</param>        /// <param name="data">数据源</param>        private void GenerateModelCode(string tableName, DataTable data)        {            StringBuilder str = new StringBuilder();            //引用信息            str.AppendLine("using System;");            str.AppendLine("using System.Collections.Generic;");            str.AppendLine("using System.Linq;");            str.AppendLine("using System.Text;");            str.AppendLine("\r");            //命名空间            str.AppendLine("namespace AutoCodeKldder");            str.AppendLine("{");            //注释信息            str.AppendLine("\t/// <summary>");            str.AppendLine("\t/// " + tableName);            str.AppendLine("\t/// <summary>");            str.AppendLine("\t[Serializable]");            str.AppendLine("\tpublic class " + tableName);            str.AppendLine("\t{");            str.AppendLine("\t\t#region 类块");            int idx = 0;            foreach (DataColumn dc in data.Columns)            {                //类型                string _type = dc.DataType.ToString();                //字段名称                string _name = dc.ColumnName;                //是否为空                if (dc.AllowDBNull && dc.DataType.IsValueType)                {                    _type += "?";                }                if (idx != 0)                {                    str.AppendLine("\r");                }                str.AppendLine("\t\tprivate " + _type + " _" + _name + ";");                str.AppendLine("\t\t/// <summary>");                str.AppendLine("\t\t/// " + _name + "");                str.AppendLine("\t\t/// </summary>");                str.AppendLine("\t\tpublic " + _type + " " + _name + "");                str.AppendLine("\t\t{");                str.AppendLine("\t\t\tget { return _" + _name + "; }");                str.AppendLine("\t\t\tset { _" + _name + " = value; }");                str.AppendLine("\t\t}");                idx++;            }            str.AppendLine("\t\t#endregion");            str.AppendLine("\t}");            str.AppendLine("}");            this.txt_modelcode.Text = str.ToString().Trim();        }        #endregion


原创粉丝点击