Skip to content

Commit

Permalink
增加MSSQL支持
Browse files Browse the repository at this point in the history
增加MSSQL支持
  • Loading branch information
supperlitt committed Jul 12, 2019
1 parent 8498fc2 commit 36eaaa1
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 7 deletions.
15 changes: 9 additions & 6 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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)


117 changes: 117 additions & 0 deletions WinGenerateCodeDB/ConnectHelper/MsSqlConnectHelper.cs
Original file line number Diff line number Diff line change
@@ -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<string> 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<string> result = new List<string>();
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<string> 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<string> result = new List<string>();
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<SqlColumnInfo> 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<SqlColumnInfo> result = new List<SqlColumnInfo>();
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;
}
}
}
2 changes: 1 addition & 1 deletion WinGenerateCodeDB/MainFrm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions WinGenerateCodeDB/WinGenerateCodeDB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<Compile Include="Code\Views\MvcViewHelper_Bootstrap.cs" />
<Compile Include="Code\Views\MvcViewHelper_EasyUI.cs" />
<Compile Include="ConnectHelper\IConnect.cs" />
<Compile Include="ConnectHelper\MsSqlConnectHelper.cs" />
<Compile Include="ConnectHelper\MySqlConnectHelper.cs" />
<Compile Include="DAL\SqlHelper.cs" />
<Compile Include="Extend\ExtendTool.cs" />
Expand Down

0 comments on commit 36eaaa1

Please sign in to comment.