C#分析数据库结构,使用XSL模板自动生成代码

来源:网络整理 责任编辑:栏目编辑 发表时间:2013-07-01 22:32 点击:
 

<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));
TableEleme

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • 用C#制作屏幕捕获程序
    • .NET程序员项目开发必知必会—Dev环境中的集成测试用例执行时上
    • 遍历ArrayList易犯错误
    • C#对XML操作:一个处理XML文件的类(1)
    • .NET简谈反射(动态调用)
    • 使用C#编写LED样式时钟控件
    • DataList嵌套问题 如何删除内层子DataList的记录
    • 怎样用C#实现完整文档打印功能
    • .NET简谈自定义事务资源管理器
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1