数据库文档CHM生成器(附文档原件与截图)
背景:
在做项目的时候,当时的数据库文档是txt格式,后来晋升为doc格式。在开发过程中,依旧觉得不方便。后来用动软生成器,发现它可以生成html和doc格式的数据库文档,html用起来相对轻松些。有一天突发奇想,若是弄成CHM文档就好了。期间用过CHM工具一段时间。人是难以满足的,懒惰是永无止尽的。后来嫌弃操作太反锁,在博客园上一位牛人博客中学习了CHM编程。此后一直以代码的方式生成CHM文档。
现在我将它做成了一个相对通用的工具,支持SQL2005及以上,Oracle。易拓展,方便有需要的朋友。
资源下载:
源码(近期会补上,想看的朋友,可以先反编译文件,或者发表你的评论哦)。
软件及文档截图:
开发流程简介:
1.读取数据库表以及表的描述信息。
SQL2008查看表名以及说明
SELECT Row_Number() over ( order by getdate() ) as 序号, case when a.colorder = 1 then d.name else '' end as 表名, case when a.colorder = 1 then isnull(f.value, '') else '' end as 表说明 FROM syscolumns a inner join sysobjects d on a.id = d.id and d.xtype = 'U' and d.name <> 'sys.extended_properties' left join sys.extended_properties f on a.id = f.major_id and f.minor_id = 0 where a.colorder = 1 and d.name<>'sysdiagrams'
SQL2008/2012查看表以及字段信息
SELECT TOP 100 PERCENT d.name as 表名, a.colorder AS 序号, a.name AS 列名, b.name AS 数据类型, a.length AS 长度, ISNULL(COLUMNPROPERTY(a.id, a.name, 'Scale'), 0) AS 小数位数, CASE WHEN COLUMNPROPERTY(a.id,a.name, 'IsIdentity') = 1 THEN '是' ELSE '' END AS 标识, CASE WHEN EXISTS (SELECT 1 FROM dbo.sysindexes si INNER JOIN dbo.sysindexkeys sik ON si.id = sik.id AND si.indid = sik.indid INNER JOIN dbo.syscolumns sc ON sc.id = sik.id AND sc.colid = sik.colid INNER JOIN dbo.sysobjects so ON so.name = si.name AND so.xtype = 'PK' WHERE sc.id = a.id AND sc.colid = a.colid) THEN '√' ELSE '' END AS 主键, CASE WHEN a.isnullable = 1 THEN '√' ELSE '' END AS 允许空, ISNULL(e.text, '') AS 默认值, ISNULL(g.[value], '') AS 列说明 FROM dbo.syscolumns a LEFT OUTER JOIN dbo.systypes b ON a.xtype = b.xusertype INNER JOIN dbo.sysobjects d ON a.id = d.id AND d.xtype = 'U' AND d.status >= 0 LEFT OUTER JOIN dbo.syscomments e ON a.cdefault = e.id LEFT OUTER JOIN sys.extended_properties g ON a.id = g.major_id AND a.colid = g.minor_id AND g.name = 'MS_Description' LEFT OUTER JOIN sys.extended_properties f ON d.id = f.major_id AND f.minor_id = 0 AND f.name = 'MS_Description' ORDER BY d.name, 序号
SQL2012查看表名以及说明
select Row_Number() over ( order by getdate() ) as 序号, t1.name as 表名,t2.value as 表说明 from sysobjects t1 left join sys.extended_properties t2 on t1.id=t2.major_id where type='u' and minor_id=0
Oracle查看表名以及说明
select ROWNUM 序号 ,ut.table_name 表名,utc.comments 表说明 from user_tables ut left join user_tab_comments utc on ut.table_name = utc.table_name order by ut.table_name
Oracle查看表以及字段信息
2.获取用户选择的表,获取该表的所有字段信息,构建html,存储在tmp临时目录。
将DataTable的数据生成为HTML
using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Text; using Chen.Ext; namespace Chen.DB { /// <summary> /// 常用功能类 /// </summary> public class DbCommon { #region 导出表数据为html格式 /// <summary> /// 导出表数据为html格式 居中表格样式 /// </summary> /// <param name="dt">DataTable,需要给TableName赋值</param> /// <param name="KeepNull">保持Null为Null值,否则为空</param> /// <param name="Path">保存路径</param> /// <param name="hasReturn">携带返回目录链接</param> /// <param name="tableDesc">携带返回目录链接</param> public static void CreateHtml(DataTable dt, bool KeepNull, string Path, bool hasReturn = true, string tableDesc = "") { var code = new StringBuilder(); code.AppendLine("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"); code.AppendLine("<html xmlns=\"http://www.w3.org/1999/xhtml\">"); code.AppendLine("<head>"); code.AppendLine(" <title>{0}</title>".FormatString(dt.TableName)); code.AppendLine(" <style type=\"text/css\">"); code.AppendLine(" body"); code.AppendLine(" {"); code.AppendLine(" font-size: 9pt;"); code.AppendLine(" }"); code.AppendLine(" .styledb"); code.AppendLine(" {"); code.AppendLine(" font-size: 14px;"); code.AppendLine(" }"); code.AppendLine(" .styletab"); code.AppendLine(" {"); code.AppendLine(" font-size: 14px;"); code.AppendLine(" padding-top: 15px;"); code.AppendLine(" }"); code.AppendLine(" a"); code.AppendLine(" {"); code.AppendLine(" color: #015FB6;"); code.AppendLine(" }"); code.AppendLine(" a:link, a:visited, a:active"); code.AppendLine(" {"); code.AppendLine(" color: #015FB6;"); code.AppendLine(" text-decoration: none;"); code.AppendLine(" }"); code.AppendLine(" a:hover"); code.AppendLine(" {"); code.AppendLine(" color: #E33E06;"); code.AppendLine(" }"); code.AppendLine(" </style>"); code.AppendLine("</head>"); code.AppendLine("<body>"); code.AppendLine(" <div style=\"text-align: center\">"); code.AppendLine(" <div>"); code.AppendLine(" <table border=\"0\" cellpadding=\"5\" cellspacing=\"0\" width=\"90%\">"); code.AppendLine(" <tr>"); code.AppendLine(" <td bgcolor=\"#FBFBFB\">"); code.AppendLine(" <table cellspacing=\"0\" cellpadding=\"5\" border=\"1\" width=\"100%\" bordercolorlight=\"#D7D7E5\" bordercolordark=\"#D3D8E0\">"); code.AppendLine(" <caption>"); code.AppendLine(" <div class=\"styletab\">{0}{1}{2}</div>".FormatString( dt.TableName, tableDesc.Length == 0 ? string.Empty : " (" + tableDesc + ") ", hasReturn ? "<a href='../数据库表目录.html' style='float: right; margin-top: 6px;'>返回目录</a>" : string.Empty)); code.AppendLine(" </caption>"); code.AppendLine(" <tr bgcolor=\"#F0F0F0\">"); //构建表头 foreach (DataColumn dc in dt.Columns) { code.AppendLine(" <td>{0}</td>".FormatString(dc.ColumnName)); } code.AppendLine(" </tr>"); //构建数据行 foreach (DataRow dr in dt.Rows) { code.AppendLine(" <tr>"); foreach (DataColumn dc in dt.Columns) { if (KeepNull && dr[dc.ColumnName] == DBNull.Value) { code.AppendLine(" <td> </td>"); } else { code.AppendLine(" <td>{0}</td>".FormatString( dr[dc.ColumnName].ToString().Trim().Length > 0 ? dr[dc.ColumnName].ToString() : " ")); } } code.AppendLine(" </tr>"); } code.AppendLine(" </table>"); code.AppendLine(" </td>"); code.AppendLine(" </tr>"); code.AppendLine(" </table>"); code.AppendLine(" </div>"); code.AppendLine(" </div>"); code.AppendLine("</body>"); code.AppendLine("</html>"); File.WriteAllText(Path, code.ToString(), Encoding.GetEncoding("gb2312")); //File.WriteAllText(Path, code.ToString(), Encoding.UTF8); } /// <summary> /// 导出表数据为html格式 Oracle导出格式 带搜索框 /// </summary> /// <param name="dt">DataTable,需要给TableName赋值</param> /// <param name="KeepNull">保持Null为Null值,否则为空</param> /// <param name="Path">保存路径</param> public static void CreateHtml2(DataTable dt, bool KeepNull, string Path) { var code = new StringBuilder(); code.AppendLine("<html>"); code.AppendLine("<head>"); code.AppendLine(" <title>J{0}</title>".FormatString(dt.TableName)); code.AppendLine(" <meta http-equiv=\"content-type\" content=\"text/html; charset=GBK\">"); code.AppendLine(" <style type=\"text/css\">"); code.AppendLine(" table"); code.AppendLine(" {"); code.AppendLine(" background-color: #F2F2F5;"); code.AppendLine(" border-width: 1px 1px 0px 1px;"); code.AppendLine(" border-color: #C9CBD3;"); code.AppendLine(" border-style: solid;"); code.AppendLine(" }"); code.AppendLine(); code.AppendLine(" td"); code.AppendLine(" {"); code.AppendLine(" color: #000000;"); code.AppendLine(" font-family: Tahoma,Arial,Helvetica,Geneva,sans-serif;"); code.AppendLine(" font-size: 9pt;"); code.AppendLine(" background-color: #EAEFF5;"); code.AppendLine(" padding: 8px;"); code.AppendLine(" background-color: #F2F2F5;"); code.AppendLine(" border-color: #ffffff #ffffff #cccccc #ffffff;"); code.AppendLine(" border-style: solid solid solid solid;"); code.AppendLine(" border-width: 1px 0px 1px 0px;"); code.AppendLine(" }"); code.AppendLine(); code.AppendLine(" th"); code.AppendLine(" {"); code.AppendLine(" font-family: Tahoma,Arial,Helvetica,Geneva,sans-serif;"); code.AppendLine(" font-size: 9pt;"); code.AppendLine(" padding: 8px;"); code.AppendLine(" background-color: #CFE0F1;"); code.AppendLine(" border-color: #ffffff #ffffff #cccccc #ffffff;"); code.AppendLine(" border-style: solid solid solid none;"); code.AppendLine(" border-width: 1px 0px 1px 0px;"); code.AppendLine(" white-space: nowrap;"); code.AppendLine(" }"); code.AppendLine(" a:link, a:visited, a:active"); code.AppendLine(" {"); code.AppendLine(" color: #015FB6;"); code.AppendLine(" text-decoration: none;"); code.AppendLine(" }"); code.AppendLine(" a:hover"); code.AppendLine(" {"); code.AppendLine(" color: #E33E06;"); code.AppendLine(" }"); code.AppendLine(" </style>"); code.AppendLine(" <script type=\"text/javascript\">"); code.AppendLine(" window.apex_search = {};"); code.AppendLine(" apex_search.init = function () {"); code.AppendLine(" this.rows = document.getElementById('data').getElementsByTagName('TR');"); code.AppendLine(" this.rows_length = apex_search.rows.length;"); code.AppendLine(" this.rows_text = [];"); code.AppendLine(" for (var i = 0; i < apex_search.rows_length; i++) {"); code.AppendLine(" this.rows_text[i] = (apex_search.rows[i].innerText) ? apex_search.rows[i].innerText.toUpperCase() : apex_search.rows[i].textContent.toUpperCase();"); code.AppendLine(" }"); code.AppendLine(" this.time = false;"); code.AppendLine(" }"); code.AppendLine(); code.AppendLine(" apex_search.lsearch = function () {"); code.AppendLine(" this.term = document.getElementById('S').value.toUpperCase();"); code.AppendLine(" for (var i = 0, row; row = this.rows[i], row_text = this.rows_text[i]; i++) {"); code.AppendLine(" row.style.display = ((row_text.indexOf(this.term) != -1) || this.term === '') ? '' : 'none';"); code.AppendLine(" }"); code.AppendLine(" this.time = false;"); code.AppendLine(" }"); code.AppendLine(); code.AppendLine(" apex_search.search = function (e) {"); code.AppendLine(" var keycode;"); code.AppendLine(" if (window.event) { keycode = window.event.keyCode; }"); code.AppendLine(" else if (e) { keycode = e.which; }"); code.AppendLine(" else { return false; }"); code.AppendLine(" if (keycode == 13) {"); code.AppendLine(" apex_search.lsearch();"); code.AppendLine(" }"); code.AppendLine(" else { return false; }"); code.AppendLine(" }</script>"); code.AppendLine("</head>"); code.AppendLine("<body onload=\"apex_search.init();\">"); code.AppendLine(" <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"); code.AppendLine(" <tbody>"); code.AppendLine(" <tr> "); code.AppendLine(" <td>"); code.AppendLine(" <input type=\"text\" size=\"30\" maxlength=\"1000\" value=\"\" id=\"S\" onkeyup=\"apex_search.search(event);\" /><input type=\"button\" value=\"Search\" onclick=\"apex_search.lsearch();\" />"); code.AppendLine(" </td>"); code.AppendLine(" <td>"); code.AppendLine(" " + dt.TableName); code.AppendLine(" </td>"); code.AppendLine(" </tr>"); code.AppendLine(" </tbody>"); code.AppendLine(" </table>"); code.AppendLine(" <br />"); code.AppendLine(" <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"); code.AppendLine(" <tr>"); foreach (DataColumn dc in dt.Columns) { code.AppendLine(" <th>{0}</th>".FormatString(dc.ColumnName)); } code.AppendLine(" </tr>"); code.AppendLine(" <tbody id=\"data\">"); foreach (DataRow dr in dt.Rows) { code.AppendLine(" <tr>"); foreach (DataColumn dc in dt.Columns) { if (KeepNull && dr[dc.ColumnName] == DBNull.Value) { //code.AppendLine(" <td>{0}</td>".FormatString(dr[dc.ColumnName].ToString())); code.AppendLine(" <td> </td>"); } else// align=\"right\" { code.AppendLine(" <td>{0}</td>".FormatString( dr[dc.ColumnName].ToString().Length > 0 ? dr[dc.ColumnName].ToString() : " ")); } } code.AppendLine(" </tr>"); } code.AppendLine(" </tbody>"); code.AppendLine(" </table>"); code.AppendLine("</body>"); code.AppendLine("</html>"); File.WriteAllText(Path, code.ToString(), Encoding.GetEncoding("gb2312")); } #endregion } }
3.在创建好所有的html之后,下一步编译成chm文档。编译之前,需要准备3个文件,hhc,hhp,hhk。分别是CHM格式文件的内容文件、编译参数文件、索引文件。文件的格式简单易懂。对比文件夹的内容以及文件夹层次结构,看一下就可以有点清楚了。这里还是简单的贴一下吧,方便大家的了解。
hhc
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> <HTML> <HEAD> <meta name="GENERATOR" content="EasyCHM.exe www.zipghost.com"> <!-- Sitemap 1.0 --> </HEAD><BODY> <OBJECT type="text/site properties"> <param name="ExWindow Styles" value="0x200"> <param name="Window Styles" value="0x800025"> <param name="Font" value="MS Sans Serif,9,0"> </OBJECT> <UL> <LI> <OBJECT type="text/sitemap"> <param name="Name" value="数据库表目录"> <param name="Local" value="数据库表目录.html"> <param name="ImageNumber" value="11"> </OBJECT> <LI> <OBJECT type="text/sitemap"> <param name="Name" value="表结构"> <param name="ImageNumber" value="1"> </OBJECT> <UL> <LI> <OBJECT type="text/sitemap"> <param name="Name" value="Code "> <param name="Local" value="表结构\Code .html"> <param name="ImageNumber" value="11"> </OBJECT> <LI> <OBJECT type="text/sitemap"> <param name="Name" value="Files 文件列表"> <param name="Local" value="表结构\Files 文件列表.html"> <param name="ImageNumber" value="11"> </OBJECT> <LI> <OBJECT type="text/sitemap"> <param name="Name" value="Menus 菜单表"> <param name="Local" value="表结构\Menus 菜单表.html"> <param name="ImageNumber" value="11"> </OBJECT> </UL> </UL> </BODY></HTML>
hhp
[OPTIONS]
CITATION=Made by Jinwin
Compatibility=1.1 or later
Compiled file=D:\数据库帮助文档.chm
Contents file=chm.HHC
COPYRIGHT=www.jinwin.net
Default topic=数据库表目录.html
Default Window=Main
Display compile notes=Yes
Display compile progress=Yes
Full-text search=Yes
Index file=chm.HHK
Title=数据库帮助文档
Enhanced decompilation=yes
[WINDOWS]
Main="数据库帮助文档","chm.hhc","chm.hhk","数据库表目录.html","数据库表目录.html",,,,,0x63520,180,0x104E, [0,0,745,509],0x0,0x0,,,,,0
[MERGE FILES]
[FILES]
E:\我的代码\Winform\DBDcoumentCreater\DBDcoumentCreater\bin\Debug\tmp\数据库表目录.html
E:\我的代码\Winform\DBDcoumentCreater\DBDcoumentCreater\bin\Debug\tmp\表结构\Code .html
E:\我的代码\Winform\DBDcoumentCreater\DBDcoumentCreater\bin\Debug\tmp\表结构\Files 文件列表.html
E:\我的代码\Winform\DBDcoumentCreater\DBDcoumentCreater\bin\Debug\tmp\表结构\Menus 菜单表.html
hhk
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> <HTML> <HEAD> <meta name="GENERATOR" content="EasyCHM.exe www.zipghost.com"> <!-- Sitemap 1.0 --> </HEAD><BODY> <OBJECT type="text/site properties"> <param name="ExWindow Styles" value="0x200"> <param name="Window Styles" value="0x800025"> <param name="Font" value="MS Sans Serif,9,0"> </OBJECT> <UL> <LI> <OBJECT type="text/sitemap"> <param name="Name" value="数据库表目录"> <param name="Local" value="数据库表目录.html"> <param name="ImageNumber" value="11"> </OBJECT> <LI> <OBJECT type="text/sitemap"> <param name="Name" value="Code "> <param name="Local" value="表结构\Code .html"> <param name="ImageNumber" value="11"> </OBJECT> <LI> <OBJECT type="text/sitemap"> <param name="Name" value="Files 文件列表"> <param name="Local" value="表结构\Files 文件列表.html"> <param name="ImageNumber" value="11"> </OBJECT> <LI> <OBJECT type="text/sitemap"> <param name="Name" value="Menus 菜单表"> <param name="Local" value="表结构\Menus 菜单表.html"> <param name="ImageNumber" value="11"> </OBJECT> </UL> </BODY></HTML>
4.chm编译前的相关文件生成完毕后,开始调用微软的hhc文件,一般系统都有,我也只在win7上做过测试。测试不足的话,还请见谅。路径为:
C:\Program Files (x86)\HTML Help Workshop\hhc.exe 。传入指定的参数,就可以得到chm文件了。
5.最后一步则是清理垃圾了。删除掉这些临时文件临时文件夹就好了。
最后:
感兴趣的朋友可以下载可执行程序看看,有需要源码的朋友留言吧,帮忙消灭万恶的零回复。近期会把源码发上来,留言的朋友优先发送。有什么不明白的,或者想问的,欢饮留言咨询。