用户名: 密码: 免费注册 忘记密码? 网站地图 | 加入收藏 | 设为首页
首页 | 新闻 | 工具 | 系统 | 办公 | 聊天 | 多媒体 | 网页 | 运营 | 平面 | 欣赏 | 数据库 | 程序 | 服务器 | 组网
网页 | 3dmax | Ghost | Windows Xp| Dreamweaver | photoshop | Flash | office | Alexa | Css | QQ | Asp | PHP | Jsp | Access
Flash MX 2004入门 | 网站推广策略 | CorelDRAW入门 | ASP学习 | 网站建设大师功 | Word入门
  iTbulo.com > 学院 > 程序开发教程 > c#教程 > c#应用教程 > 正文
C#分析数据库结构,使用XSL模板自动生成代码
iTbulo.COM 2007-4-27 动态网站制作指南()

<html>
<head>
<TITLE>分析数据库结构,自动生成代码</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<frameset cols="237,767" rows="*">
<frame src="dbxml.aspx">
<frame name="code" src="about:blank">
</frameset>
</html>
########################### dbxml.aspx 文件内容,该文件没有C#代码文件 #############
<script language="C#" runat ="server">
System.Xml.XmlDocument myCfgXML = new System.Xml.XmlDocument();
// 获得系统配置字符串
string GetAppConfig(string strKey)
{
System.Xml.XmlElement cfgElement = myCfgXML.SelectSingleNode ("//setting[@key='" + strKey + "']" )
as System.Xml.XmlElement ;
if( cfgElement == null )
return "";
else
return cfgElement.InnerText ;
}

// 判断字符串是否是空白字符串
bool isBlankString(string strText )
{
if(strText != null)
{
int iCount;
for(iCount=0;iCount<strText.Length ;iCount++)
{
if(System.Char.IsWhiteSpace ( strText[iCount])==false)
return false;
}
}
return true;
}
void Page_Load(Object sender, EventArgs e)
{
// 加载系统配置文件
myCfgXML.Load(this.MapPath(".") + "\\dbxmlcfg.xml");
string strType = this.Request["type"];
string strXSL = "main.xml";

if(strType == null)
strType = "querytable";
System.Xml.XmlDocument myDoc = new System.Xml.XmlDocument();
myDoc.LoadXml("<dbxml />");
string strConnection = GetAppConfig("conndbxml");
System.Text.Encoding myEncode = System.Text.Encoding.GetEncoding(936);

if(isBlankString(strConnection)==false)
{
using(System.Data.OleDb.OleDbConnection myConn = new System.Data.OleDb.OleDbConnection(strConnection))
{
myConn.Open();
if(myConn.State == System.Data.ConnectionState.Open )
{
string strSQL = GetAppConfig(strType + "_" + myConn.Provider);
if(isBlankString(strSQL)==false)
{
using(System.Data.OleDb.OleDbCommand myCmd = myConn.CreateCommand())
{
string strTableName = null;
if(strType.Equals("queryfield"))
{
// 修正SQL语句
string strTableList = this.Request.Form["tablelist"];
string []strTables = strTableList.Split(",".ToCharArray());
strXSL = System.Web.HttpUtility.UrlPathEncode(this.Request.Form["template"] ) + ".xml";
strTableList = null;
for(int iCount = 0 ; iCount < strTables.Length ; iCount ++ )
{
if(isBlankString(strTables[iCount])==false)
{
if(strTableList == null)
strTableList = "'" + strTables[iCount] + "'";
else
strTableList = strTableList + ",'" + strTables[iCount] + "'";
}
}
strSQL = strSQL.Replace("#tablelist", strTableList);
myCmd.CommandText = strSQL ;
string strLastTableName = null;
string strFieldName = null;
System.Xml.XmlElement TableElement = null;
System.Data.OleDb.OleDbDataReader myReader = myCmd.ExecuteReader();
while(myReader.Read())
{
strTableName = myReader[0].ToString().ToUpper();
if(strTableName.Equals(strLastTableName)==false)
{
// 填充表说明元素
strLastTableName = strTableName ;
TableElement = myDoc.CreateElement("table");
TableElement.SetAttribute("tablename", strTableName);
myDoc.DocumentElement.AppendChild(TableElement);
}
// 填充字段说明元素
System.Xml.XmlElement FieldElement = myDoc.CreateElement("field");
FieldElement.SetAttribute("fieldname", myReader[1].ToString());
FieldElement.SetAttribute("fieldtype", myReader[2].ToString());
FieldElement.SetAttribute("fieldwidth", myReader[3].ToString());
FieldElement.SetAttribute("isstring", (myReader[2].ToString().ToUpper().IndexOf("CHAR")>=0?"1":"0"));
strFieldName = myReader[1].ToString();
int iLen = myEncode.GetByteCount(strFieldName);
if(iLen < 20)
FieldElement.SetAttribute("fixname", strFieldName + new string(' ', 20 - iLen));
TableElement.AppendChild(FieldElement);
}
myReader.Close();
}
else
{
// 填充模板列表
string [] strFileNames = System.IO.Directory.GetFiles(this.Server.MapPath("."),"temp_*.xml");
for(int iCount = 0 ; iCount < strFileNames.Length ; iCount ++ )
{
System.Xml.XmlElement tempXML = myDoc.CreateElement("template");
tempXML.SetAttribute("key",System.IO.Path.GetFileNameWithoutExtension(strFileNames[iCount]));
myDoc.DocumentElement.AppendChild(tempXML);
}
// 填充表名列表
myCmd.CommandText = strSQL ;
System.Data.OleDb.OleDbDataReader myReader = myCmd.ExecuteReader();
System.Xml.XmlElement TableElement = null;
while(myReader.Read())
{
TableElement = myDoc.CreateElement("table");
myDoc.DocumentElement.AppendChild(TableElement);
strTableName = myReader[0].ToString();
TableElement.SetAttribute("name", strTableName );
TableElement.SetAttribute("count", myReader[1].ToString());
int iLen = myEncode.GetByteCount(strTableName);
if(iLen < 20 )
TableElement.SetAttribute("fixname",strTableName + new string(' ', 20 - iLen));
}
myReader.Close();
}
}
}
}
myConn.Close();
}
}
// 输出文档
this.Response.ContentType = "text/xml";
this.Response.ContentEncoding = myEncode ;
this.Response.Write("<?xml version=\"1.0\" encoding=\"GB2312\" ?>");
this.Response.Write("<?xml-stylesheet type=\"text/xsl\" href=\"" + strXSL + "\"?>");
this.Response.Write(myDoc.DocumentElement.OuterXml);
}
</script>

[1] [2] [3] [4] [5] [6] [7] 下一页

文章搜索
相关资讯
相关文章 相关下载
关于正则表达式匹配无异常资源耗尽的解决方案
使用c#捕获windows的关机事件
C#图像放大问题解决方法
如何用C#来部署数据库
C# Operate Excel File
焦点信息