自己编码实现数据库的映射实体的代码生成器_MySQL

php中文网
发布: 2016-06-01 13:29:40
原创
1090人浏览过

bitsCN.com

以前很多时候都是使用codesmith或动软生成相关的数据库访问代码(不喜欢使用持久化框架),可以codesmith对中文的支持不好,新下载的6.5的版本,怎样都不能正确显示中文,动软代码生成器的表字段的注释永远都显示不了(李天平的败笔),有几个属性如mysql的smallint,tinyint,得重新更改选项的映射表,太麻烦了。虽然说动软现在提供了模块生成功能,批量生成中对表的重命名也不能够,功能有点弱呀,于是自己写个代码生成器,按自己的规则去生成,注释也出来了,多了注释提示,对开发帮助很大的。代码如下:

 

        private void CreateModel()        {            string connectionString = "server=127.0.0.1;port=3306;User Id=root;Password=root;database=ipbroadcast;Connect Timeout=60;Treat Tiny As Boolean=False";            MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(connectionString);            conn.Open();            DataTable table = conn.GetSchema("TABLES");            foreach (DataRow row in table.Rows)            {                DataTable sheetTable = GetSchemaTable(conn, row);                string tName = row["TABLE_NAME"].ToString();                CreateEntityFile(conn, tName, sheetTable);            }            conn.Close();        }        private DataTable GetSchemaTable(MySqlConnection conn, DataRow row)        {            string sheetName = row["TABLE_Name"].ToString();            MySqlCommand com = conn.CreateCommand();            com.CommandType = CommandType.Text;            com.CommandText = "Select * From " + sheetName;            IDataReader reader = com.ExecuteReader(CommandBehavior.SchemaOnly);            DataTable table = reader.GetSchemaTable();            com.Dispose();            reader.Close();            return table;        }        private void CreateEntityFile(MySqlConnection conn, string tName, DataTable table)        {            //定义生成文件的路径            string tableName = tName;            string schemaName = "";            string colName = "";            string path = @"F:/IPBroadcastModel/Model1";            if (!Directory.Exists(path))                Directory.CreateDirectory(path);            MySqlCommand com = conn.CreateCommand();            com.CommandType = CommandType.Text;            Dictionary<string, string> dict = new Dictionary<string, string>();            bool isGetComment = false;            //写文件            string clsName = "Ety" + tableName.Substring(3, 1).ToUpper() + tableName.Substring(4);            string fileName = clsName + ".cs";            string fullName = Path.Combine(path, fileName);            using (FileStream fs = new FileStream(fullName, FileMode.Create))            {                StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);                sw.WriteLine("using System;");                sw.WriteLine("using System.Text;");                sw.WriteLine("using System.Collections.Generic; ");                sw.WriteLine("using System.Data;");                sw.WriteLine("namespace AEBell.DBManager");                sw.WriteLine("{");                sw.WriteLine("/t[Serializable]");                sw.WriteLine("/tpublic class " + clsName);                sw.WriteLine("/t{");                foreach (DataRow row in table.Rows)                {                    //tableName = row["BaseTableName"].ToString();                    colName = row["ColumnName"].ToString();                    schemaName = row["BaseSchemaName"].ToString();                    if (!isGetComment)                    {                        isGetComment = true;                        GetFielComment(tableName, schemaName, com, dict);                    }                    sw.WriteLine("/t/t/// <summary>");                    sw.WriteLine("/t/t/// " + dict[colName]);                    sw.WriteLine("/t/t/// </summary>");                    Type info = row["DataType"] as Type;                    string declear = info.Name;                    if (declear.ToUpper() == "SBYTE")   //为了适应动软。                        declear = "Byte";                    bool isNull = (bool)row["AllowDBNull"];                    if (isNull && info.BaseType.Name == "ValueType")                        declear += "?";                    sw.WriteLine("/t/tpublic " + declear + " " + colName.Substring(0, 1).ToUpper() + colName.Substring(1));                    sw.WriteLine("/t/t{");                    sw.WriteLine("/t/t/tget;");                    sw.WriteLine("/t/t/tset;");                    sw.WriteLine("/t/t}");                }                sw.WriteLine("/t}");                sw.WriteLine("}");                sw.Close();            }        }        private static void GetFielComment(string tableName, string schemaName, MySqlCommand com, Dictionary<string, string> dict)        {            string comment = "";            com.CommandText = "Select COLUMN_NAME,DATA_TYPE, COLUMN_COMMENT From  INFORMATION_SCHEMA.COLUMNS where table_name ='"                + tableName + "' AND table_schema = '" + schemaName + "'"; // AND column_name LIKE '" + colName + "'";            IDataReader reader = com.ExecuteReader();            while (reader.Read())            {                string colName = reader.GetString(0);                comment = reader.GetString(2);                dict[colName] = comment;            }            reader.Close();        }
登录后复制

 虽然实现不是很严谨,DAL和BLL层也没有实现,但有需要之人是可以很快实现的。不对之处,请指正。

成新网络商城购物系统
成新网络商城购物系统

使用模板与程序分离的方式构建,依靠专门设计的数据库操作类实现数据库存取,具有专有错误处理模块,通过 Email 实时报告数据库错误,除具有满足购物需要的全部功能外,成新商城购物系统还对购物系统体系做了丰富的扩展,全新设计的搜索功能,自定义成新商城购物系统代码功能代码已经全面优化,杜绝SQL注入漏洞前台测试用户名:admin密码:admin888后台管理员名:admin密码:admin888

成新网络商城购物系统 0
查看详情 成新网络商城购物系统
bitsCN.com
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号