开源项目:数据库表结构生成文档工具
Posted IT技术分享社区
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了开源项目:数据库表结构生成文档工具相关的知识,希望对你有一定的参考价值。
目录
一、软件介绍
今天给大家分享我自己编写的数据库表结构文档生成工具,方便大家在实际开发当中,可以很方便导出业务系统的表结构,也可以作为项目验收文档中数据库设计文档使用。这样可以大大减少编写数据库表结构文档的时间,有需要的朋友欢迎下载或者沟通交流!
二、技术框架
- 编程语言:C# ( Net Framework4.5.5)
- 数据库技术框架:Dapper
- 导出Word文档:NPOI
- 访问方式:WinForm窗体应用程序,Windows操作系统可以直接运行
三、功能介绍
- 支持SQLServer、mysql(5.7、8.0)、SQLite 三种类型的数据,持续更新
- 支持Word、html、MarkDown 三种格式的导出
- 导出内容包含数据表(字段详情、字段注释、长度、默认值等)、创建表脚本、视图及视图脚本、存储过程及脚本
- 支持生成文档的同时直接打开文档
- 支持数据库备份(目前只支持SQLServer导出bak备份文件)
四、代码展示
1、获取数据库信息部分代码
/// <summary>
/// 获取数据库字符串
/// </summary>
/// <param name="servername"></param>
/// <param name="uid"></param>
/// <param name="pwd"></param>
/// <param name="db"></param>
/// <returns></returns>
public string GetConnectioning(string servername, string uid, string pwd, string db, string port)
return string.Format("server=0;uid=1;pwd=2;database=3", servername, uid, pwd, db);
/// <summary>
/// 获取数据库列表
/// </summary>
/// <param name="conStr"></param>
/// <returns></returns>
public List<string> GetDBNameList(string conStr)
//List<DBName> list =new List<DBName>();
string sql = "select [name] from master.dbo.sysdatabases where DBId>6 Order By [Name] ";
try
using (SqlConnection connection = new SqlConnection(conStr))
var list = connection.Query<string>(sql).ToList();
return list;
catch
return null;
public List<DBModel> GetDBList(string conStr)
//List<DBName> list =new List<DBName>();
string sql = "select [name] from master.dbo.sysdatabases where DBId>6 Order By [Name] ";
try
using (SqlConnection connection = new SqlConnection(conStr))
var list = connection.Query<DBModel>(sql).ToList();
return list;
catch
return null;
/// <summary>
/// 获取特定数据库的表名列表
/// </summary>
/// <param name="conStr"></param>
/// <returns></returns>
public List<TableModel> GetDBTableList(string conStr, string dbName = "")
var list = new List<TableModel>();
//string sql = "SELECT TABLE_NAME as name FROM INFORMATION_SCHEMA.TABLES where TABLE_TYPE='BASE TABLE' ";
string sql = "select a.name AS tableName,CONVERT(NVARCHAR(100),isnull(g.[value],'')) AS tableDesc from sys.tables a left join sys.extended_properties g on (a.object_id = g.major_id AND g.minor_id = 0)";
try
using (SqlConnection connection = new SqlConnection(conStr))
list = connection.Query<TableModel>(sql).ToList();
catch
return list;
/// <summary>
/// 获取特定数据库里面的存储过程
/// </summary>
/// <param name="conStr"></param>
/// <param name="db"></param>
/// <returns></returns>
public List<ProcModel> GetProcList(string conStr, string dbName = "")
var list = new List<ProcModel>();
string sql = @" select name as procName, (select text from syscomments where id=OBJECT_ID(name)) as proDerails
from dbo.sysobjects o where OBJECTPROPERTY(id, N'IsProcedure') = 1 order by name ";
try
using (SqlConnection connection = new SqlConnection(conStr))
list = connection.Query<ProcModel>(sql).ToList();
catch
return list;
/// <summary>
/// 获取特定数据库里面的视图
/// </summary>
/// <param name="conStr"></param>
/// <param name="db"></param>
/// <returns></returns>
public List<ViewModel> GetViewList(string conStr, string dbName = "")
var list = new List<ViewModel>();
string sql = @" select name as viewName, (select text from syscomments where id=OBJECT_ID(name)) as viewDerails
from dbo.sysobjects o where OBJECTPROPERTY(id, N'IsView') = 1 order by name ";
try
using (SqlConnection connection = new SqlConnection(conStr))
list = connection.Query<ViewModel>(sql).ToList();
catch
return list;
/// <summary>
/// 获取字段的信息
/// </summary>
/// <param name="tableName"></param>
/// <param name="conStr"></param>
/// <returns></returns>
public List<TableDetail> GetTableDetail(string tableName, string conStr, string dbName = "")
var list = new List<TableDetail>();
StringBuilder sb = new StringBuilder();
sb.Append("SELECT [index] = a.colorder, Title = a.name, isMark = CASE WHEN COLUMNPROPERTY(a.id, a.name, 'IsIdentity') = 1 THEN '1' ELSE '0' END, ");
sb.Append("isPK = CASE WHEN EXISTS(SELECT 1 FROM sysobjects WHERE xtype = 'PK' AND parent_obj = a.id AND name IN(SELECT name FROM sysindexes WHERE indid IN(SELECT indid FROM sysindexkeys WHERE id = a.id AND colid = a.colid)) ) THEN '1' ELSE '0' END, ");
sb.Append(" FieldType = b.name,fieldLenth = COLUMNPROPERTY(a.id, a.name, 'PRECISION'),isAllowEmpty = CASE WHEN a.isnullable = 1 THEN '1' ELSE '0' END, defaultValue = ISNULL(e.text, ''), fieldDesc = ISNULL(g.[value], '') ");
sb.Append("FROM syscolumns a LEFT JOIN systypes b ON a.xusertype = b.xusertype INNER JOIN sysobjects d ON a.id = d.id AND d.xtype = 'U' AND d.name <> 'dtproperties' LEFT JOIN syscomments e ON a.cdefault = e.id ");
sb.Append("LEFT JOIN sys.extended_properties g ON a.id = G.major_id AND a.colid = g.minor_id LEFT JOIN sys.extended_properties f ON d.id = f.major_id AND f.minor_id = 0");
//--如果只查询指定表,加上此红色where条件,tablename是要查询的表名;去除红色where条件查询说有的表信息
sb.Append("WHERE d.name = '" + tableName + "' ORDER BY a.id, a.colorder, d.name");
try
using (SqlConnection connection = new SqlConnection(conStr))
list = connection.Query<TableDetail>(sb.ToString()).ToList();
catch
return list;
2、导出Html文档代码
/// <summary>
/// 生成html文件
/// </summary>
/// <param name="list"></param>
/// <param name="conStr"></param>
/// <param name="db"></param>
/// <param name="type"></param>
public void CreateToHtml(List<TableModel> list, string conStr, string db, int type, List<string> checkList)
StringBuilder sb = new StringBuilder();
sb.Append("<html><meta charset=\\"utf-8\\" /><meta http-equiv = \\"Content-Language\\" content = \\"zh-CN\\" >");
sb.Append("<head><title>数据库说明文档</title><body>");
sb.Append("<style type=\\"text/css\\">\\n");
sb.Append("body font-size: 9pt;\\n");
sb.Append(".styledb font-size: 14px; \\n");
sb.Append(".styletab font-size: 14px;padding-top: 15px; \\n</style></head><body>");
sb.Append("<h1 style=\\"text-align:center;\\">" + db + "数据库说明文档</h1>");
GetDBService(type);
#region 创建一个表格
if (checkList.Where(m => m.Equals("表")).Count() > 0)
sb.Append("<h2>一、表结构</h2>");
sb.Append("");
sb.Append("");
if (list.Count > 0)
foreach (var item in list)
if (item.tableDesc != null && item.tableDesc != "")
sb.Append("<h3>表名:" + item.tableName + "(" + item.tableDesc + ")</h3>");
else
sb.Append("<h3>表名:" + item.tableName + "</h3>");
sb.Append(" <table cellspacing=\\"0\\" cellpadding=\\"5\\" border=\\"1\\" width=\\"100%\\" bordercolorlight=\\"#4F7FC9\\" bordercolordark=\\"#D3D8E0\\">");
sb.Append("<thead bgcolor=\\"#E3EFFF\\"> <th>序号</th><th>字段名称</th><th>标识</th><th>主键</th><th>字段类型</th><th>字段长度</th><th>允许空值</th><th>字段默认值</th><th>字段备注</th></thead>");
sb.Append("<tbody>");
//从第二行开始 因为第一行是表头
int i = 1;
var tabledetaillist = service.GetTableDetail(item.tableName, conStr, db);
if (tabledetaillist != null && tabledetaillist.Count > 0)
foreach (var itm in tabledetaillist)
sb.Append("<tr>");
sb.Append("<td>" + itm.index + "</td>");
sb.Append("<td>" + itm.Title + "</td>");
sb.Append("<td>" + itm.isMark + "</td>");
sb.Append("<td>" + itm.isPK + "</td>");
sb.Append("<td>" + itm.FieldType + "</td>");
sb.Append("<td>" + itm.fieldLenth + "</td>");
sb.Append("<td>" + itm.isAllowEmpty + "</td>");
sb.Append("<td>" + itm.defaultValue + "</td>");
sb.Append("<td>" + itm.fieldDesc + "</td>");
sb.Append("</tr>");
i++;
sb.Append("</tbody></table>");
sb.Append("<h4>" + item.tableName + "建表脚本</h4><br/>");
sb.Append("<span>" + service.GetTableSQL(item.tableName, conStr) + "</span>");
#endregion
#region 存储过程
if (checkList.Where(m => m.Equals("存储过程")).Count() > 0)
List<ProcModel> proclist = new List<ProcModel>();
proclist = service.GetProcList(conStr, db);
sb.Append("<h2>二、存储过程</h2>");
if (proclist != null && proclist.Count > 0)
foreach (var item in proclist)
sb.Append("<h3>存储过程名称:" + item.procName + "</h3>");
sb.Append("<span>" + item.proDerails + "</span>");
#endregion
#region 视图
if (checkList.Where(m => m.Equals("视图")).Count() > 0)
List<ViewModel> viewlist = new List<ViewModel>();
viewlist = service.GetViewList(conStr, db);
sb.Append("<h2>三、视图</h2>");
if (viewlist.Count > 0)
foreach (var item in viewlist)
sb.Append("<h3>视图名称:" + item.viewName + "</h3>");
sb.Append("<span>" + item.viewDerails + "</span>");
#endregion
sb.Append("</body></html>");
sb.ToString();
string filename = db + "-数据库说明文档";//文件名
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "html";
saveDialog.Filter = "html文件|*.html";
saveDialog.FileName = filename;
saveDialog.ShowDialog();
filename = saveDialog.FileName;
if (filename.IndexOf(":") < 0) return; //被点了取消
StreamWriter sw1 = new StreamWriter(saveDialog.FileName, false);
sw1.WriteLine(sb);
sw1.Close();
System.Diagnostics.Process.Start(filename);
五、运行效果
应用程序主界面
支持三种生成文档类型:每次只能选择一种,推荐使用markdown格式
Word文档生成效果
Html文档生成效果
MarkDown文档效果
针对SQLServer数据库备份
六、项目开源地址
GitHub:https://github.com/hgmsq/SqlToDocTool
Gitee:https://gitee.com/hgm1989/SqlToDocTool
Gitcode:https://gitcode.net/xishining/SqlToDocTool
一款好用的数据库表结构文档生成工具
下面介绍一款:简洁好用的数据库表结构文档工具screw
,支持MySQL/MariaDB/SqlServer/Oracle/PostgreSQL/TIDB/CacheDB 数据库。
主要代码示例:
/**
* 文档生成
*/
void documentGeneration() {
//数据源
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
hikariConfig.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/database");
hikariConfig.setUsername("root");
hikariConfig.setPassword("password");
//设置可以获取tables remarks信息
hikariConfig.addDataSourceProperty("useInformationSchema", "true");
hikariConfig.setMinimumIdle(2);
hikariConfig.setMaximumPoolSize(5);
DataSource dataSource = new HikariDataSource(hikariConfig);
//生成配置
EngineConfig engineConfig = EngineConfig.builder()
//生成文件路径
.fileOutputDir(fileOutputDir)
//打开目录
.openOutputDir(true)
//文件类型
.fileType(EngineFileType.HTML)
//生成模板实现
.produceType(EngineTemplateType.freemarker)
//自定义文件名称
.fileName("自定义文件名称").build();
//忽略表
ArrayList<String> ignoreTableName = new ArrayList<>();
ignoreTableName.add("test_user");
ignoreTableName.add("test_group");
//忽略表前缀
ArrayList<String> ignorePrefix = new ArrayList<>();
ignorePrefix.add("test_");
//忽略表后缀
ArrayList<String> ignoreSuffix = new ArrayList<>();
ignoreSuffix.add("_test");
ProcessConfig processConfig = ProcessConfig.builder()
//指定生成逻辑、当存在指定表、指定表前缀、指定表后缀时,将生成指定表,其余表不生成、并跳过忽略表配置
//根据名称指定表生成
.designatedTableName(new ArrayList<>())
//根据表前缀生成
.designatedTablePrefix(new ArrayList<>())
//根据表后缀生成
.designatedTableSuffix(new ArrayList<>())
//忽略表名
.ignoreTableName(ignoreTableName)
//忽略表前缀
.ignoreTablePrefix(ignorePrefix)
//忽略表后缀
.ignoreTableSuffix(ignoreSuffix).build();
//配置
Configuration config = Configuration.builder()
//版本
.version("1.0.0")
//描述
.description("数据库设计文档生成")
//数据源
.dataSource(dataSource)
//生成配置
.engineConfig(engineConfig)
//生成配置
.produceConfig(processConfig)
.build();
//执行生成
new DocumentationExecute(config).execute();
}
插件代码示例:
<!--数据库文档生成 start-->
<plugin>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-maven-plugin</artifactId>
<version>1.0.5</version>
<dependencies>
<!-- HikariCP -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
<!--mysql driver-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<!--oracle driver-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.3.0.0</version>
</dependency>
<dependency>
<groupId>cn.easyproject</groupId>
<artifactId>orai18n</artifactId>
<version>12.1.0.2.0</version>
</dependency>
</dependencies>
<configuration>
<username>test</username> <!--数据库用户名-->
<password>test</password> <!--数据库密码-->
<!--数据库驱动 Mysql-->
<!-- <driverClassName>com.mysql.cj.jdbc.Driver</driverClassName>-->
<!--数据库驱动 Oracle-->
<driverClassName>oracle.jdbc.driver.OracleDriver</driverClassName>
<!--jdbc Mysql Url-->
<jdbcUrl>jdbc:mysql://127.0.0.1:3306/xxxx</jdbcUrl>
<!--jdbc Oracle Url-->
<jdbcUrl>jdbc:oracle:thin:@127.0.0.1:1521:orcl</jdbcUrl>
<!--生成文件类型 HTML WORD MD -->
<!-- <fileType>HTML</fileType>-->
<!-- <fileType>WORD</fileType> -->
<fileType>MD</fileType>
<!--打开文件输出目录-->
<openOutputDir>false</openOutputDir>
<!--生成模板-->
<produceType>freemarker</produceType>
<!--文档名称 为空时:将采用[数据库名称-描述-版本号]作为文档名称-->
<fileName></fileName>
<!--描述-->
<description>数据库文档生成</description>
<!--版本-->
<version>1.0.0</version>
<!--标题-->
<title>数据库文档</title>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<!--数据库文档生成 end-->
结果演示:
配置完成数据库的连接参数后,双击运行screw:run
,生成的文档将会在项目工程的doc文件夹
下面。
效果图:
以上是关于开源项目:数据库表结构生成文档工具的主要内容,如果未能解决你的问题,请参考以下文章