这是本博的第100篇文章,日期是2009-9-9,开博至今正好2年多点,曾一度一月才写一篇,可还是坚持下来,坚持到现在,偶尔翻翻几天前、几周前、几个月前、1年前的文章,欣慰的发现自己真的在进步,这里都是我的脚印。其实在这之前有在CSDN和Bolgger上写过,要么是不好用,要么就是懒得几个月都不写,结果是只写了不到30篇就没有继续下去,不过现在看来真的是在这里扎根了下来 :) 在园子里看的文章何止百篇,累积应该不少于五千篇,大家都在辛勤的记录和分享着,感激之余也努力着试图回报给园子里。我的博客并没有写最新的技术,也没有写大型的架构设计,有的只是在小公司里面摸爬滚打的一点点经验,觉得帮助较大的放首页,小技巧什么的就放新手区,在关键字上也会花点功夫以便更加容易被寻求帮助的朋友搜到,总之,谢谢大家,我也会一如既往的继续写下去 :)
---------------------------------------------------------------------------------------------------
前言
在小公司待过的朋友可能常常会碰到收烂摊子的事情,比如程序写完了,没有文档,尤其是数据库文档,那么你接受的第一件事情可能就是数据库文档了,如果按一个个表一个个字段来复制来写文档的话表一多估计会累死,而且容易出错,还得反复校验,且枯燥无味,这里我写了一个小程序来帮助大家快速准确的来完成这一任务。
参考文章
1. ^全^ 获取SQL SERVER2000/2005、MySql、Oracle元数据的SQL语句 [SQL语句来自CodeSmith]
2. C# 操作Excel之旁门左道 [ C# | Excel ]
正文
一、目标
下图是我们要实现的目标:
二、实现
原理非常简单,首先制作样式模版,可以参照文章2,将排版好的Word另存为html,然后复制粘贴到aspx页面中,然后从数据库读取表以及字段信息,动态的插入表名和字段信息。
2.1 SqlSchemaProvider.cs
//
// 作 者:农民伯伯
// 邮 箱:over140@gmail.com
// 博 客:http://over140.cnblogs.com/
// 时 间:2009-9-9
// 描 述:获取SQL SERVER 元数据
//
//==============================================================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
public sealed class SqlSchemaProvider
{
#region Constructor
public SqlSchemaProvider(string connectstring)
{
ConnectString = connectstring;
}
#endregion
#region GetTableColumns
public IList<ColumnInfo> GetTableColumns(string tableName)
{
IList<ColumnInfo> result = new List<ColumnInfo>();
SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder(ConnectString);
using (SqlConnection conn = new SqlConnection(scsb.ConnectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand(SQL2000_GetTableColumns, conn);
cmd.Parameters.Add(new SqlParameter("@DatabaseName", scsb.InitialCatalog));
cmd.Parameters.Add(new SqlParameter("@SchemaName", "dbo"));
cmd.Parameters.Add(new SqlParameter("@TableName", tableName));
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
{
result.Add(new ColumnInfo()
{
Name = reader.GetString(0),
DataType = reader.GetString(1),
Length = reader.GetInt32(3),
Nullable = reader.GetString(6).Trim().Equals("YES") ? true : false,
DefaultValue = reader.IsDBNull(7) ? "" : reader[7].ToString(),
Identity = reader.GetInt32(8),
IdentitySeed = Convert.ToInt32(reader.GetString(12)),
IdentityIncrement = Convert.ToInt32(reader.GetString(13)),
ColumnDesc = reader.GetString(17)
});
}
reader.Close();
}
return result;
}
public IList<string> GetTables()
{
IList<string> result = new List<string>();
SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder(ConnectString);
using (SqlConnection conn = new SqlConnection(scsb.ConnectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand(SQL2000_GetTables, conn);
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
{
result.Add(reader.GetString(0));
}
reader.Close();
}
return result;
}
#region Type Maps
private string GetCSharpType(string type)
{
if (string.IsNullOrEmpty(type))
return "string";
string reval = string.Empty;
switch (type.ToLower())
{
case "varchar":
case "nchar":
case "ntext":
case "text":
case "char":
case "nvarchar":
reval = "string";
break;
case "int":
reval = "int";
break;
case "smallint":
reval = "Int16";
break;
case "bigint":
reval = "Int64";
break;
case "float":
reval = "double";
break;
case "bit":
reval = "bool";
break;
case "decimal":
case "smallmoney":
case "money":
case "numeric":
reval = "decimal";
break;
case "binary":
reval = "System.Byte[]";
break;
case "real":
reval = "System.Single";
break;
case "datetime":
case "smalldatetime":
case "timestamp":
reval = "System.DateTime";
break;
case "tinyint":
reval = "System.Byte";
break;
case "uniqueidentifier":
reval = "System.Guid";
break;
case "p_w_picpath":
case "varbinary":
reval = "System.Byte[]";
break;
case "Variant":
reval = "Object";
break;
default:
reval = "string";
break;
}
return reval;
}
#endregion
#endregion
#region SQL Templates
#region GetTableColumns
private const string SQL2000_GetTables = @"
SELECT
object_name(so.id) AS OBJECT_NAME,
user_name(so.uid) AS USER_NAME,
so.type AS TYPE,
so.crdate AS DATE_CREATED,
fg.file_group AS FILE_GROUP,
so.id AS OBJECT_ID
FROM
dbo.sysobjects so
LEFT JOIN (
SELECT
s.groupname AS file_group,
i.id AS id
FROM dbo.sysfilegroups s
INNER JOIN dbo.sysindexes i
ON i.groupid = s.groupid
WHERE i.indid < 2
) AS fg
ON so.id = fg.id
WHERE
so.type = N'U'
AND permissions(so.id) & 4096 <> 0
AND ObjectProperty(so.id, N'IsMSShipped') = 0
ORDER BY user_name(so.uid), object_name(so.id)";
private const string SQL2000_GetTableColumns = @"
SELECT
clmns.[name] AS [Name],
usrt.[name] AS [DataType],
ISNULL(baset.[name], N'') AS [SystemType],
CAST(CASE WHEN baset.[name] IN (N'char', N'varchar', N'binary', N'varbinary', N'nchar', N'nvarchar') THEN clmns.prec ELSE clmns.length END AS INT) AS [Length],
CAST(clmns.xprec AS TINYINT) AS [NumericPrecision],
CAST(clmns.xscale AS INT) AS [NumericScale],
CASE CAST(clmns.isnullable AS BIT) WHEN 1 THEN 'YES' ELSE 'NO' END AS [Nullable],
defaults.text AS [DefaultValue],
CAST(COLUMNPROPERTY(clmns.id, clmns.[name], N'IsIdentity') AS INT) AS [Identity],
CAST(COLUMNPROPERTY(clmns.id, clmns.[name], N'IsRowGuidCol') AS INT) AS IsRowGuid,
CAST(COLUMNPROPERTY(clmns.id, clmns.[name], N'IsComputed') AS INT) AS IsComputed,
CAST(COLUMNPROPERTY(clmns.id, clmns.[name], N'IsDeterministic') AS INT) AS IsDeterministic,
CAST(CASE COLUMNPROPERTY(clmns.id, clmns.[name], N'IsIdentity') WHEN 1 THEN IDENT_SEED(QUOTENAME(stbl.[name]) + '.' + QUOTENAME(tbl.[name])) ELSE 0 END AS NVARCHAR(40)) AS [IdentitySeed],
CAST(CASE COLUMNPROPERTY(clmns.id, clmns.[name], N'IsIdentity') WHEN 1 THEN IDENT_INCR(QUOTENAME(stbl.[name]) + '.' + QUOTENAME(tbl.[name])) ELSE 0 END AS NVARCHAR(40)) AS [IdentityIncrement],
cdef.[text] AS ComputedDefinition,
clmns.[collation] AS Collation,
CAST(clmns.colid AS int) AS ObjectId,
isnull(prop.value, '') AS ColumnDesc
FROM
dbo.sysobjects AS tbl
INNER JOIN dbo.sysusers AS stbl ON stbl.[uid] = tbl.[uid]
INNER JOIN dbo.syscolumns AS clmns ON clmns.id=tbl.id
LEFT JOIN dbo.systypes AS usrt ON usrt.xusertype = clmns.xusertype
LEFT JOIN dbo.sysusers AS sclmns ON sclmns.uid = usrt.uid
LEFT JOIN dbo.systypes AS baset ON baset.xusertype = clmns.xtype and baset.xusertype = baset.xtype
LEFT JOIN db.syscomments AS defaults ON defaults.id = clmns.cdefault
LEFT JOIN dbo.syscomments AS cdef ON cdef.id = clmns.id AND cdef.number = clmns.colid
LEFT OUTER JOIN sysproperties prop ON clmns.id = prop.id AND clmns.colid = prop.smallid
WHERE
(tbl.[type] = 'U' OR tbl.[type] = 'S')
AND stbl.[name] = 'dbo'
AND tbl.[name] = @TableName
ORDER BY
clmns.colorder";
#endregion
#endregion
#region Properties
public string ConnectString { get; set; }
#endregion
}