博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asp.net数据库表结构导出Word
阅读量:5934 次
发布时间:2019-06-19

本文共 4452 字,大约阅读时间需要 14 分钟。

.aspx

    Untitled Page        

 .aspx.cs

protected void Page_Load(object sender, EventArgs e)        {            if (!IsPostBack)            {                ltlBody.Text = GetTableStr("FubonConnectionString");            }                  }        string GetTableStr(string conStr)        {            DataBase db = null;            if (string.IsNullOrEmpty(conStr))                db = new DataBase();            else                db = new DataBase(conStr);            string sql = @"select TableName = O.name,	OWNER = USER_NAME(O.uid),		FieldId = C.colid,    FieldName = C.name,	FieldType = T.name		+ CASE			WHEN T.name IN (N'decimal',N'numeric')				THEN N'(' + CAST(C.prec as varchar) + N',' + CAST(C.scale as varchar) + N')'			WHEN T.name = N'float' OR T.name like N'%char'								   OR T.name like N'%binary'				THEN N'(' + CAST(C.prec as varchar) + N')'			ELSE N''		  END,    AllowNulls =           CASE				WHEN C.isnullable = 1 THEN N''				ELSE N' NOT'		  END	+ N' NULL',	FieldDescription = ISNULL(PFD.value, ''),	DefileLength = C.length,	FiledDefault = ISNULL(DF.text, N''),	IsIDENTITY = COLUMNPROPERTY(O.id, C.name, N'IsIdentity'),	IsComputed = COLUMNPROPERTY(O.id, C.name, N'IsComputed'),	IsROWGUID = COLUMNPROPERTY(O.id, C.name, N'IsRowGuidCol'),	IsPrimaryKey = CASE							WHEN OPK.xtype IS NULL THEN 0						ELSE 1					END	FROM sysobjects O		JOIN syscolumns C			ON C.id = O.id AND OBJECTPROPERTY(O.id, N'IsUserTable') = 1		JOIN systypes T			ON T.xusertype = C.xusertype		LEFT JOIN syscomments DF			ON DF.id = C.cdefault		LEFT JOIN sys.extended_properties PFD 			ON PFD.major_id = C.id AND PFD.minor_id = C.colid				LEFT JOIN sysindexkeys IDXK			ON IDXK.id = O.id AND IDXK.colid = C.colid		LEFT JOIN sysindexes IDX			ON IDX.indid = IDXK.indid AND IDX.id =IDXK.id AND IDX.indid NOT IN(0,255)		LEFT JOIN sysobjects OPK			ON OPK.parent_obj = O.id AND OPK.name = IDX.name AND OBJECTPROPERTY(OPK.id, N'IsPrimaryKey') = 1ORDER BY O.name, C.colid";            DataTable dt = db.GetDataSet(sql).Tables[0];            if (dt == null || dt.Rows.Count == 0) return "";            string tableName = dt.Rows[0]["TableName"].ToString();            const string tableStr = "
"; StringBuilder sbTable = new StringBuilder(); sbTable.Append(tableStr + tableName + "" + columnStr); foreach (DataRow dr in dt.Rows) { if (tableName != dr["TableName"].ToString()) { sbTable.Append("
TableName "; const string columnStr = "
Id ColumnName DataType DefaultValue IsPrimaryKey IsIdentity AllowNulls Description
"); tableName = dr["TableName"].ToString(); sbTable.Append(tableStr + tableName + "" + columnStr); } sbTable.AppendFormat("{0}{1}{2}{3}{4}{5}{6}{7}", dr["FieldId"].ToString(), dr["FieldName"].ToString(), dr["FieldType"].ToString(), dr["FiledDefault"].ToString().Replace("((", "").Replace("))", ""), dr["IsPrimaryKey"].ToString() == "1" ? "√" : "", dr["IsIDENTITY"].ToString().ToString() == "1" ? "√" : "", dr["AllowNulls"].ToString().Trim() == "NULL" ? "√" : "", dr["FieldDescription"].ToString()); } sbTable.Append(""); return sbTable.ToString(); } protected void btnExport_Click(object sender, EventArgs e) { string header = string.Format("
{0}
({1})
", "数据库字典", DateTime.Now.ToString("yyyy年MM月dd日")); ResponseWrite(header + "
" + GetTableStr("FubonConnectionString")); } void ResponseWrite(string str) { Response.Clear(); Response.Buffer = true; Response.Charset = "UTF-8"; Response.ContentEncoding = Encoding.GetEncoding("UTF-8"); string filename = string.Format("数据库字典_{0}.doc", DateTime.Now.ToString("yyyyMMdd")); Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(filename,System.Text.Encoding.UTF8)); Response.ContentType = "application/ms-word"; Response.Write(@" "); Response.Write(str); Response.End(); }

 

转载地址:http://fbctx.baihongyu.com/

你可能感兴趣的文章
Active Directory之域时钟
查看>>
Linux SAMBA服务器重启后自动挂载失效
查看>>
top动态查看负载
查看>>
oracle11g 静默安装
查看>>
Linux 进程管理命令之jobs
查看>>
Exchange server 2013(十二)IRM故障排除
查看>>
Oracle-merge用法详解(不稳定)
查看>>
windos下mysql迁移到LINUX数据冷备份及恢复
查看>>
centos7 搭建 SVN 服务器
查看>>
第七节 项目沟通管理、项目合同管理
查看>>
我的地盘我做主(一)——RAID
查看>>
android SQLite数据库使用实例
查看>>
vCloud Director Enterprise Cloud 5.5部署(二)
查看>>
整理NSDate,NSTimeInterval和时间间隔问题
查看>>
图片压缩
查看>>
深入理解程序设计-使用Linux 32bit 汇编语言
查看>>
监控网络流量
查看>>
linux下安装zookeeper(集群版)
查看>>
计算机基础知识
查看>>
低调的张近东高调督战,苏宁这次是真的要拼了
查看>>