首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Dotnet频道
中国IT教育
Google
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> C# >> 正文

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



####################### main.xml ################################## 
<?xml version="1.0" encoding="gb2312" ?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:output method="xml" indent="yes" /> 
<xsl:template match="/*"> 
<html> 
<head> 
<title></title> 
<style> 
select2{ font-family: "宋体"; font-size: 12px} 
body{ font-family: "宋体"; font-size: 12px} 
table{ width:100%; border-collapse:collapse; 
border: 1px #CC0066 solid; font-family: "宋体"; 
font-size: 12px} 
.tablehead{background-color:#CCCCFF} 
td{ border: 1px #CC0066 solid} </style> 
</head> 
<body leftmargin='1' rightmargin='1' topmargin="1"> 
<form name="frm" target="code" method="POST" action="dbxml.aspx?type=queryfield"> 
模板<select name="template"> 
<xsl:for-each select="template"> 
<option> 
<xsl:attribute name="value"> 
<xsl:value-of select="@key" /> 
</xsl:attribute> 
<xsl:value-of select="@key" /> 
</option> 
</xsl:for-each> 
</select> 
<input type="submit" value="提交" /> 
<table> 
<tr class="tablehead"> 
<td nowrap="1">选择</td> 
<td nowrap="1">表名</td> 
<td nowrap="1">字段个数</td> 
</tr> 
<xsl:for-each select="table"> 
<tr> 
<td nowrap="1"> 
<input type="checkbox" name="tablelist" style="width:13;height:13"> 
<xsl:attribute name="value"> 
<xsl:value-of select="@name" /> 
</xsl:attribute> 
</input> 
</td> 
<td> 
<xsl:value-of select="@name" /> 
</td> 
<td> 
<xsl:value-of select="@count" /> 
</td> 
</tr> 
</xsl:for-each> 
</table> 
</form> 
</body> 
</html> 
</xsl:template> 
</xsl:stylesheet> 

########################## temp_CSharp.xml 内容 ############################################ 
<?xml version="1.0" encoding="utf-8" ?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:output method="xml" indent="yes" /> 
<xsl:template match="/"> 
<html> 
<head></head> 
<body> 
<xsl:for-each select="*/table"> 
<br />-------------- 文件名 <xsl:value-of select="@tablename" />.cs ----------------------------- 
<pre style=" background-color:gainsboro"> 
//----------------------------------------------------------------------------- 
<xsl:text disable-output-escaping="yes"> 
/// <summary></xsl:text> 
/// 数据库表 <xsl:value-of select="@tablename" /> 操作对象 
/// 编制: 代码生成器 
/// 时间: 
<xsl:text disable-output-escaping="yes"> /// </summary></xsl:text> 
public class Struct<xsl:value-of select="@tablename" /> : CommonStruct 
{ private const string c_TableName ="<xsl:value-of select="@tablename" />"; 
// 定义数据库字段变量 //////////////////////////////////////////////////////////////// 
<xsl:for-each select="*"> 
<xsl:variable name="csharptype"> 
<xsl:choose> 
<xsl:when test="@isstring='1'">string </xsl:when> 
<xsl:when test="boolean('true')">int </xsl:when> 
</xsl:choose> 
</xsl:variable> 
<xsl:variable name="lowfieldname"> 
<xsl:value-of select="translate(@fieldname,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" /> 
</xsl:variable> 
private <xsl:value-of select="$csharptype" /> m_<xsl:value-of select="@fixname" /> ; // 字段 <xsl:value-of select="@cname" /> 
</xsl:for-each> 

// 定义属性 /////////////////////////////////////////////////////////// 
<xsl:for-each select="*"> 
<xsl:variable name="csharptype"> 
<xsl:choose> 
<xsl:when test="@isstring='1'">string </xsl:when> 
<xsl:when test="boolean('true')">int </xsl:when> 
</xsl:choose> 
</xsl:variable> 
/// <xsl:text disable-output-escaping="yes"><summary></xsl:text> 
/// 设置/返回数据库字段属性 <xsl:value-of select="@cname" /> 
/// <xsl:text disable-output-escaping="yes"></summary></xsl:text> 
/// <xsl:text disable-output-escaping="yes"><returns></xsl:text><xsl:value-of select="@cname" /><xsl:text disable-output-escaping="yes"> </returns></xsl:text> 
public <xsl:value-of select="$csharptype" /> m<xsl:value-of select="@fieldname" /> 

get{ return m_<xsl:value-of select="@fieldname" /> ;} 
set{ m_<xsl:value-of select="@fieldname" /> = value ;} 

</xsl:for-each> 


new public static string getTableName() 
{return c_TableName ;} 
new public static string getSelectSQL() 

return "Select <xsl:for-each select="*"> 
<xsl:value-of select="normalize-space(@fieldname)" /> 
<xsl:if test="position() != last()">,</xsl:if> 
</xsl:for-each> From " + c_TableName ; 

new public static string getTypeName() 

return "<xsl:value-of select="translate(@tablename,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" />"; 

new public static string SearchKey(string strKey) 

return getSelectSQL() + " Where SEQ =" + strKey ; 

public override bool SetCommandParams( System.Data.OleDb.OleDbCommand myCmd,bool SetValues ) 

if(myCmd!= null) 

myCmd.Parameters.Clear (); 
<xsl:for-each select="*"> 
myCmd.Parameters.Add("<xsl:value-of select="normalize-space(@fieldname)" />",System.Data.OleDb.OleDbType.<xsl:if test="@isstring='1'">
VarWChar</xsl:if><xsl:if test="@isstring='0'">Integer</xsl:if>);
</xsl:for-each> 
if(SetValues) 

<xsl:for-each select="*"> 
myCmd.Parameters[<xsl:value-of select="position()-1" />].Value = m_<xsl:value-of select="@fieldname" /> ; // 字段 <xsl:value-of select="@cname" /> 
</xsl:for-each> 

return true; 

return false; 

public override bool SetInsertCommand( System.Data.OleDb.OleDbCommand myCmd) 

if(myCmd != null) 

myCmd.CommandText ="Insert Into " + c_TableName 
+ " ( <xsl:for-each select="*">[<xsl:value-of select="normalize-space(@fieldname)" />]<xsl:if test="position() != last()">,</xsl:if> 
</xsl:for-each>) Values (<xsl:for-each select="*">?<xsl:if test="position() != last()">,</xsl:if> 
</xsl:for-each>)"; 
return this.SetCommandParams(myCmd,true); 

return false; 

public override bool SetUpdateCommand(System.Data.OleDb.OleDbCommand myCmd) 

if(myCmd != null) 

myCmd.CommandText ="Update " + c_TableName 
+ " Set <xsl:for-each select="*">[<xsl:value-of select="normalize-space(@fieldname)" />]=? <xsl:if test="position() != last()">,</xsl:if></xsl:for-each> Where SEQ=" + m_SEQ ; 
return this.SetCommandParams(myCmd,true); 

return false; 

public override bool SelectRS(System.Data.OleDb.OleDbDataReader myReader) 

try 

if(myReader != null) 

if (myReader.FieldCount==5) 

<xsl:for-each select="*"> 
m_<xsl:value-of select="@fixname" />
 = Convert.To<xsl:if test="@isstring='1'">String</xsl:if><xsl:if test="@isstring='0'">Int32
</xsl:if>(myReader[<xsl:value-of select="position()-1" />]);
</xsl:for-each> 
return true; 


上一页  [1] [2] [3] [4] 下一页

【责编:Peng】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·WEB程序开发--ASP.NET和PHP、JSP究竟学哪个?
 ·五步带你入门XML
 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航