From 36eaaa11fbc8a56da71b9ef1b8bc014e10cfcf89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B6=85=E7=BA=A7=5F=E5=B0=8F=E5=B0=8F?= Date: Fri, 12 Jul 2019 16:19:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0MSSQL=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加MSSQL支持 --- README.MD | 15 ++- .../ConnectHelper/MsSqlConnectHelper.cs | 117 ++++++++++++++++++ WinGenerateCodeDB/MainFrm.cs | 2 +- WinGenerateCodeDB/WinGenerateCodeDB.csproj | 1 + 4 files changed, 128 insertions(+), 7 deletions(-) create mode 100644 WinGenerateCodeDB/ConnectHelper/MsSqlConnectHelper.cs diff --git a/README.MD b/README.MD index eb10cc2..3a61119 100644 --- a/README.MD +++ b/README.MD @@ -4,11 +4,14 @@ 多种生成方式:可以用sql生成,可以接入数据库服务器生成。 参考网址:https://www.cnblogs.com/Supperlitt/p/11171799.html -![](https://github.com/supperlitt/WebAutoCodeOnline/image/db_1.png) -![](https://github.com/supperlitt/WebAutoCodeOnline/image/db_2.png) -![](https://github.com/supperlitt/WebAutoCodeOnline/image/db_3.png) -![](https://github.com/supperlitt/WebAutoCodeOnline/image/db_4.png) -![](https://github.com/supperlitt/WebAutoCodeOnline/image/db_proj.png) -![](https://github.com/supperlitt/WebAutoCodeOnline/image/db_web.png) +调整说明:· +2019-7-12 1.增加AutoDB对MSSQL的支持 + +![](https://github.com/supperlitt/WebAutoCodeOnline/blob/master/image/db_1.png) +![](https://github.com/supperlitt/WebAutoCodeOnline/blob/master/image/db_2.png) +![](https://github.com/supperlitt/WebAutoCodeOnline/blob/master/image/db_3.png) +![](https://github.com/supperlitt/WebAutoCodeOnline/blob/master/image/db_4.png) +![](https://github.com/supperlitt/WebAutoCodeOnline/blob/master/image/db_proj.png) +![](https://github.com/supperlitt/WebAutoCodeOnline/blob/master/image/db_web.png) diff --git a/WinGenerateCodeDB/ConnectHelper/MsSqlConnectHelper.cs b/WinGenerateCodeDB/ConnectHelper/MsSqlConnectHelper.cs new file mode 100644 index 0000000..a7db6be --- /dev/null +++ b/WinGenerateCodeDB/ConnectHelper/MsSqlConnectHelper.cs @@ -0,0 +1,117 @@ +using MySql.Data.MySqlClient; +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.SqlClient; +using System.Linq; +using System.Text; + +namespace WinGenerateCodeDB +{ + public class MsSqlConnectHelper : IConnect + { + public List GetDbList(string server, string name, string pwd, int port = 3306) + { + string selectSql = "select * from master.dbo.SysDatabases"; + string mysqlConnectionStr = string.Format("server={0};uid={1};pwd={2};database=master;", server, name, pwd, port); + List result = new List(); + using (SqlConnection sqlcn = new SqlConnection(mysqlConnectionStr)) + { + SqlCommand sqlcm = new SqlCommand(); + sqlcm.Connection = sqlcn; + sqlcm.CommandText = selectSql; + sqlcm.CommandType = CommandType.Text; + sqlcn.Open(); + using (SqlDataReader sqldr = sqlcm.ExecuteReader()) + { + while (sqldr.Read()) + { + result.Add(sqldr["name"] == DBNull.Value ? "" : sqldr["name"].ToString()); + } + } + } + + return result; + } + + public List GetTableList(string server, string name, string pwd, int port, string dbname) + { + string mysqlConnectionStr = string.Format("server={0};uid={1};pwd={2};database={4};", server, name, pwd, port, dbname); + string selectSql = string.Format("SELECT name FROM SysObjects Where XType='U'", dbname); + List result = new List(); + using (SqlConnection sqlcn = new SqlConnection(mysqlConnectionStr)) + { + SqlCommand sqlcm = new SqlCommand(); + sqlcm.Connection = sqlcn; + sqlcm.CommandText = selectSql; + sqlcm.CommandType = CommandType.Text; + sqlcn.Open(); + using (SqlDataReader sqldr = sqlcm.ExecuteReader()) + { + while (sqldr.Read()) + { + string tablename = sqldr["name"].ToString(); + + result.Add(tablename); + } + } + } + + return result; + } + + public List GetColumnsList(string server, string name, string pwd, int port, string dbname, string tablename) + { + string mysqlConnectionStr = string.Format("server={0};uid={1};pwd={2};database={4};", server, name, pwd, port, dbname); + string selectSql = string.Format(@"SELECT +表名=case when a.colorder=1 then d.name else '' end, +字段名=a.name, +标识=case when COLUMNPROPERTY(a.id,a.name,'IsIdentity')=1 then '√' else '' end, +主键=case when exists(SELECT 1 FROM sysobjects where xtype= 'PK' and name in ( +SELECT name FROM sysindexes WHERE indid in( +SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid +))) then '√' else '' end, +类型=b.name, +占用字节数=a.length, +长度=COLUMNPROPERTY(a.id,a.name, 'PRECISION'), +小数位数=isnull(COLUMNPROPERTY(a.id,a.name, 'Scale'),0), +允许空=case when a.isnullable=1 then '√'else '' end, +默认值=isnull(e.text, ''), +字段说明=isnull(g.[value], '') +FROM syscolumns a +left join systypes b on a.xtype=b.xusertype +inner join sysobjects d on a.id=d.id and d.xtype= 'U' and d.name <> 'dtproperties' and d.name = '{0}' +left join syscomments e on a.cdefault=e.id +left join sys.extended_properties g on a.id=g.major_id and a.colid=g.minor_id and g.name='MS_Description' +order by a.id,a.colorder", tablename); + List result = new List(); + using (SqlConnection sqlcn = new SqlConnection(mysqlConnectionStr)) + { + SqlCommand sqlcm = new SqlCommand(); + sqlcm.Connection = sqlcn; + sqlcm.CommandText = selectSql; + sqlcm.CommandType = CommandType.Text; + sqlcn.Open(); + using (SqlDataReader sqldr = sqlcm.ExecuteReader()) + { + while (sqldr.Read()) + { + SqlColumnInfo info = new SqlColumnInfo(); + info.Name = sqldr["字段名"] == DBNull.Value ? "" : sqldr["字段名"].ToString(); + info.DbType = sqldr["类型"] == DBNull.Value ? "" : sqldr["类型"].ToString(); + info.Length = sqldr["长度"] == DBNull.Value ? "" : sqldr["长度"].ToString(); + info.IsMainKey = (sqldr["主键"] == DBNull.Value ? "" : sqldr["主键"].ToString()) == "√"; + info.IsAllowNull = (sqldr["允许空"] == DBNull.Value ? "" : sqldr["允许空"].ToString()) == "√"; + info.DefaultValue = sqldr["默认值"] == DBNull.Value ? "[null]" : sqldr["默认值"].ToString(); + info.IsAutoIncrement = (sqldr["标识"] == DBNull.Value ? "" : sqldr["标识"].ToString()) == "√"; + info.Comment = sqldr["字段说明"] == DBNull.Value ? "" : sqldr["字段说明"].ToString(); + + result.Add(info); + } + } + } + + return result; + } + } +} diff --git a/WinGenerateCodeDB/MainFrm.cs b/WinGenerateCodeDB/MainFrm.cs index fe11007..fe457bb 100644 --- a/WinGenerateCodeDB/MainFrm.cs +++ b/WinGenerateCodeDB/MainFrm.cs @@ -165,7 +165,7 @@ private void btnConnect_Click(object sender, EventArgs e) else if (rbtnMsSql.Checked) { dbType = 1; - connect = null; + connect = new MsSqlConnectHelper(); } else if (rbtnSqlite.Checked) { diff --git a/WinGenerateCodeDB/WinGenerateCodeDB.csproj b/WinGenerateCodeDB/WinGenerateCodeDB.csproj index cad5fc4..a435b14 100644 --- a/WinGenerateCodeDB/WinGenerateCodeDB.csproj +++ b/WinGenerateCodeDB/WinGenerateCodeDB.csproj @@ -78,6 +78,7 @@ +