代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C#
】
实现数据库的映射实体的代码生成器
作者:
玉玲
/ 发布于
2016/9/14
/
834
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(); }
试试其它关键字
同语言下
.
C#实现的html内容截取
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
.
实现对图片上传的接收
.
去除字符串中的空格,回车,换行符转变成‘;’在按‘
.
按照回车换行符分割字符串
.
文件MD5码 比较,检测文件是否一样
可能有用的
.
C#实现的html内容截取
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
.
实现对图片上传的接收
.
去除字符串中的空格,回车,换行符转变成‘;’在按‘
.
按照回车换行符分割字符串
.
文件MD5码 比较,检测文件是否一样
玉玲
贡献的其它代码
(
18
)
.
Django下解决小文件下载
.
Linq查询出结果集中重复数据
.
实现银行卡号的校验
.
查询同一表内多字段同时重复记录
.
验证IPV6地址
.
实现数据库的映射实体的代码生成器
.
合并结构相同,不同,或多个DataTable
.
把两个数组合并,并删除第二个元素
.
ajax解析xml数据
.
布尔母函数
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3