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层也没有实现,但有需要之人是可以很快实现的。不对之处,请指正。
bitsCN.com
在数据库优化中,应根据查询需求选择索引策略:1.当查询涉及多个列且条件顺序固定时,使用复合索引;2.当查询涉及多个列但条件顺序不固定时,使用多个单列索引。复合索引适用于优化多列查询,单列索引则适合单列查询。

要优化MySQL慢查询,需使用slowquerylog和performance_schema:1.启用slowquerylog并设置阈值,记录慢查询;2.利用performance_schema分析查询执行细节,找出性能瓶颈并优化。

MySQL和SQL是开发者必备技能。1.MySQL是开源的关系型数据库管理系统,SQL是用于管理和操作数据库的标准语言。2.MySQL通过高效的数据存储和检索功能支持多种存储引擎,SQL通过简单语句完成复杂数据操作。3.使用示例包括基本查询和高级查询,如按条件过滤和排序。4.常见错误包括语法错误和性能问题,可通过检查SQL语句和使用EXPLAIN命令优化。5.性能优化技巧包括使用索引、避免全表扫描、优化JOIN操作和提升代码可读性。

MySQL异步主从复制通过binlog实现数据同步,提升读性能和高可用性。1)主服务器记录变更到binlog;2)从服务器通过I/O线程读取binlog;3)从服务器的SQL线程应用binlog同步数据。

MySQL是一个开源的关系型数据库管理系统。1)创建数据库和表:使用CREATEDATABASE和CREATETABLE命令。2)基本操作:INSERT、UPDATE、DELETE和SELECT。3)高级操作:JOIN、子查询和事务处理。4)调试技巧:检查语法、数据类型和权限。5)优化建议:使用索引、避免SELECT*和使用事务。

MySQL的安装和基本操作包括:1.下载并安装MySQL,设置根用户密码;2.使用SQL命令创建数据库和表,如CREATEDATABASE和CREATETABLE;3.执行CRUD操作,使用INSERT,SELECT,UPDATE,DELETE命令;4.创建索引和存储过程以优化性能和实现复杂逻辑。通过这些步骤,你可以从零开始构建和管理MySQL数据库。

InnoDBBufferPool通过将数据和索引页加载到内存中来提升MySQL数据库的性能。1)数据页加载到BufferPool中,减少磁盘I/O。2)脏页被标记并定期刷新到磁盘。3)LRU算法管理数据页淘汰。4)预读机制提前加载可能需要的数据页。

MySQL适合初学者使用,因为它安装简单、功能强大且易于管理数据。1.安装和配置简单,适用于多种操作系统。2.支持基本操作如创建数据库和表、插入、查询、更新和删除数据。3.提供高级功能如JOIN操作和子查询。4.可以通过索引、查询优化和分表分区来提升性能。5.支持备份、恢复和安全措施,确保数据的安全和一致性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3汉化版
中文版,非常好用

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

禅工作室 13.0.1
功能强大的PHP集成开发环境