大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > C#技巧 > C#一步一步开发自己的自动代码生成工具之五:DAL层模板

C#一步一步开发自己的自动代码生成工具之五:DAL层模板

关键词:自动代码生成工具C#DAL层模板  阅读(807) 赞(40)

[摘要]本文主要介绍如何使用C#一步一步开发自己的自动代码生成工具之五:DAL层模板,与大家分享学习。

2.DAL层模板


<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="/">
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Data;

using <xsl:value-of select="TabelModel/NameSpacePri"/>Model;

namespace <xsl:value-of select="TabelModel/NameSpacePri"/>DAL
{
 public partial class <xsl:value-of select="TabelModel/ModelName"/>DAL
 {
  
  #region 属性/构造函数

  /// <summary>
  /// 默认构造函数
  ///</summary>
  public <xsl:value-of select="TabelModel/ModelName"/>DAL()
  {

  }

  #endregion

  #region 数据检测
  
  <xsl:if test="TabelModel/HasPK = 'true'">
  
  /// <summary>
  /// 是否存在指定主键的数据
  ///</summary>
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true']">
  /// <param><xsl:attribute name="name"><xsl:value-of select="VarName"/></xsl:attribute>指定<xsl:value-of select="Mark"/></param>
  </xsl:for-each> 
  /// <returns>True/False</returns>
  public bool ExistsWithPK(<xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/VarType"/><xsl:text> </xsl:text><xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/VarName"/><xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true'][position()>1]">, <xsl:value-of select="VarType"/><xsl:text> </xsl:text><xsl:value-of select="VarName"/></xsl:for-each>)
  {
   using (SQLHelper DbEngine = new SQLHelper())
   {
    return ExistsWithPK(DbEngine<xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true']">, <xsl:value-of select="VarName"/></xsl:for-each>);
   }
  }

  /// <summary>
  /// 是否存在指定主键的数据
  ///</summary>
  /// <param name="DbEngine">指定数据库引擎</param>
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true']">
  /// <param>
   <xsl:attribute name="name">
    <xsl:value-of select="VarName"/>
   </xsl:attribute><xsl:value-of select="Mark"/></param>
  </xsl:for-each>
  /// <returns>True/False</returns>
  internal bool ExistsWithPK(SQLHelper DbEngine<xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true']">, <xsl:value-of select="VarType"/><xsl:text> </xsl:text><xsl:value-of select="VarName"/></xsl:for-each>)
  {
   StringBuilder strSQL = new StringBuilder();
   strSQL.Append(" SELECT COUNT(1) FROM <xsl:value-of select="TabelModel/TableName"/> ");
   strSQL.Append(" WHERE <xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/DbFieldName"/> = @<xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/DbFieldName"/> ");
   <xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true'][position()>1]">
   strSQL.Append(" AND <xsl:value-of select="DbFieldName"/> = @<xsl:value-of select="DbFieldName"/> ");
   </xsl:for-each>
   SqlParameter[] parameters =
   {
    new SqlParameter("@<xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/DbFieldName"/>", <xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/SqlParamType"/>, <xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/Length"/>)
   <xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true'][position()>1]">
    ,new SqlParameter("@<xsl:value-of select="DbFieldName"/>", <xsl:value-of select="SqlParamType"/>, <xsl:value-of select="Length"/>)
   </xsl:for-each>
   };
   <xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true']">
   parameters[<xsl:value-of select="position()-1"/>].Value = <xsl:value-of select="VarName"/>;
   </xsl:for-each>

   return DbEngine.Exists(strSQL.ToString(), parameters);
  }
  </xsl:if>

  <xsl:if test="TabelModel/HasLogicKey = 'true'">

  /// <summary>
  /// 是否存在指定逻辑主键的数据
  /// </summary>
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true']">
  /// <param><xsl:attribute name="name"><xsl:value-of select="VarName"/></xsl:attribute><xsl:value-of select="Mark"/></param>
  </xsl:for-each>
  /// <returns>True/False</returns>
  public bool ExistsWithLogicKey(<xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/VarType"/><xsl:text> </xsl:text><xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/VarName"/><xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][position()>1]">, <xsl:value-of select="VarType"/><xsl:text> </xsl:text><xsl:value-of select="VarName"/></xsl:for-each>)
  {
   using (SQLHelper DbEngine = new SQLHelper())
   {
    return ExistsWithLogicKey(DbEngine<xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true']">, <xsl:value-of select="VarName"/></xsl:for-each>);
   }
  }

  /// <summary>
  /// 是否存在指定逻辑主键的数据
  /// </summary>
  /// <param name="DbEngine">指定数据库引擎</param>
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true']">
  /// <param><xsl:attribute name="name"><xsl:value-of select="VarName"/></xsl:attribute><xsl:value-of select="Mark"/></param>
  </xsl:for-each>
  /// <returns>True/False</returns>
  internal bool ExistsWithLogicKey(SQLHelper DbEngine<xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true']">, <xsl:value-of select="VarType"/><xsl:text> </xsl:text><xsl:value-of select="VarName"/></xsl:for-each>)
  {
   StringBuilder strSQL = new StringBuilder();
   strSQL.Append(" SELECT COUNT(1) FROM <xsl:value-of select="TabelModel/TableName"/> ");
   strSQL.Append(" WHERE <xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/DbFieldName"/> = @<xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/DbFieldName"/> ");
   <xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][position()>1]">
   strSQL.Append(" AND <xsl:value-of select="DbFieldName"/> = @<xsl:value-of select="DbFieldName"/> ");
   </xsl:for-each>
   SqlParameter[] parameters =
   {
    new SqlParameter("@<xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/DbFieldName"/>", <xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/SqlParamType"/>, <xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/Length"/>)
   <xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][position()>1]">
    ,new SqlParameter("@<xsl:value-of select="DbFieldName"/>", <xsl:value-of select="SqlParamType"/>, <xsl:value-of select="Length"/>)
   </xsl:for-each>
   };
   <xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true']">
   parameters[<xsl:value-of select="position()-1"/>].Value = <xsl:value-of select="VarName"/>;
   </xsl:for-each>

   return DbEngine.Exists(strSQL.ToString(), parameters);
  }
  </xsl:if>
  
  /// <summary>
  /// 是否存在满足指定条件的数据
  ///</summary>
  /// <param name="strWhere">条件(不包含“Where”关键字)</param>
  /// <returns>True/False</returns>
  public bool ExistsWithParam(string strWhere)
  {
   using (SQLHelper DbEngine = new SQLHelper())
   {
    return ExistsWithParam(DbEngine, strWhere);
   }
  }

  /// <summary>
  /// 是否存在满足指定条件的数据
  ///</summary>
  /// <param name="DbEngine">指定数据库引擎</param>
  /// <param name="strWhere">条件(不包含“Where”关键字)</param>
  /// <returns>True/False</returns>
  internal bool ExistsWithParam(SQLHelper DbEngine, string strWhere)
  {
   StringBuilder strSQL = new StringBuilder();
   strSQL.Append("SELECT COUNT(1) FROM <xsl:value-of select="TabelModel/TableName"/> ");
   if (strWhere.Length <xsl:text disable-output-escaping="yes">></xsl:text> 0)
   {
    strSQL.Append(" WHERE " + strWhere);
   }

   return DbEngine.Exists(strSQL.ToString());
  }

  #endregion

  #region 数据检索

  /// <summary>
  /// 根据DataTable填充数据实体列表
  /// </summary>
  /// <param name="dt">DataTable数据</param>
  /// <returns>数据实体列表</returns>
  private List <xsl:text disable-output-escaping="yes">&lt;</xsl:text><xsl:value-of select="TabelModel/ModelName"/>Model<xsl:text disable-output-escaping="yes">&gt;</xsl:text> GetListFromDataTable(DataTable dt)
  {
   List<xsl:text disable-output-escaping="yes">&lt;</xsl:text><xsl:value-of select="TabelModel/ModelName"/>Model<xsl:text disable-output-escaping="yes">&gt;</xsl:text> list = new List<xsl:text disable-output-escaping="yes">&lt;</xsl:text><xsl:value-of select="TabelModel/ModelName"/>Model<xsl:text disable-output-escaping="yes">&gt;</xsl:text>();
   if (dt != null <xsl:text disable-output-escaping="yes">&amp;&amp;</xsl:text> dt.Rows.Count <xsl:text disable-output-escaping="yes">&gt;</xsl:text> 0)
   {
    foreach (DataRow dr in dt.Rows)
    {
     list.Add(GetModelFromRow(dr));
    }
   }
   return list;
  }

  /// <summary>
  /// 根据DataRow填充数据实体对象
  /// </summary>
  /// <param name="dr">DataRow数据</param>
  /// <returns>对应数据实体对象</returns>
  private <xsl:value-of select="TabelModel/ModelName"/>Model GetModelFromRow(DataRow dr)
  {
   if (dr != null)
   {
    <xsl:value-of select="TabelModel/ModelName"/>Model model = new <xsl:value-of select="TabelModel/ModelName"/>Model();
    
    <xsl:for-each select="TabelModel/Fields/FieldModel">
    if (dr["<xsl:value-of select="DbFieldName"/>"] != DBNull.Value)
    <xsl:choose>
     <xsl:when test="VarType='byte[]'">model.<xsl:value-of select="VarName"/> = <xsl:value-of select="ToVarConvertMethod"/>dr["<xsl:value-of select="DbFieldName"/>"];</xsl:when>
     <xsl:otherwise>model.<xsl:value-of select="VarName"/> = <xsl:value-of select="ToVarConvertMethod"/>(dr["<xsl:value-of select="DbFieldName"/>"]);</xsl:otherwise>
    </xsl:choose>
    </xsl:for-each>

    return model;
   }
   else
   {
    return null;
   }
  }

  <xsl:if test="TabelModel/HasPK = 'true'">
   
  /// <summary>
  /// 根据主键检索数据实体
  /// </summary>
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true']">
  /// <param>
    <xsl:attribute name="name">
     <xsl:value-of select="VarName"/>
    </xsl:attribute>
    <xsl:value-of select="Mark"/>
   </param>
  </xsl:for-each>
  /// <returns>检索结果,数据实体</returns>
  public <xsl:value-of select="TabelModel/ModelName"/>Model Get<xsl:value-of select="TabelModel/ModelName"/>ByPK(<xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/VarType"/><xsl:text> </xsl:text><xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/VarName"/><xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true'][position()>1]">, <xsl:value-of select="VarType"/><xsl:text> </xsl:text><xsl:value-of select="VarName"/></xsl:for-each>)
  {
   DataRow dr = GetRowByPK(<xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/VarName"/><xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true'][position()>1]">, <xsl:value-of select="VarName"/></xsl:for-each>);
   return GetModelFromRow(dr);
  }

  /// <summary>
  /// 根据主键检索数据实体
  /// </summary>
  /// <param name="DbEngine">数据引擎</param>
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true']">
  /// <param>
    <xsl:attribute name="name">
     <xsl:value-of select="VarName"/>
    </xsl:attribute>
    <xsl:value-of select="Mark"/>
   </param>
  </xsl:for-each>
  /// <returns>检索结果,数据实体</returns>
  internal <xsl:value-of select="TabelModel/ModelName"/>Model Get<xsl:value-of select="TabelModel/ModelName"/>ByPK(SQLHelper DbEngine<xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true']">, <xsl:value-of select="VarType"/><xsl:text> </xsl:text><xsl:value-of select="VarName"/></xsl:for-each>)
  {
   DataRow dr = GetRowByPK(DbEngine<xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true']">, <xsl:value-of select="VarName"/></xsl:for-each>);
   return GetModelFromRow(dr);
  }

  </xsl:if>
  
  <xsl:if test="TabelModel/HasLogicKey = 'true'">
   
  /// <summary>
  /// 根据逻辑主键检索数据实体
  /// </summary>
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true']">
  /// <param>
    <xsl:attribute name="name">
     <xsl:value-of select="VarName"/>
    </xsl:attribute>
    <xsl:value-of select="Mark"/>
   </param>
  </xsl:for-each>
  /// <returns>检索结果,数据实体</returns>
  public <xsl:value-of select="TabelModel/ModelName"/>Model Get<xsl:value-of select="TabelModel/ModelName"/>ByLogicKey(<xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/VarType"/><xsl:text> </xsl:text><xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/VarName"/><xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][position()>1]">, <xsl:value-of select="VarType"/><xsl:text> </xsl:text><xsl:value-of select="VarName"/></xsl:for-each>)
  {
   DataRow dr = GetRowByLogicKey(<xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/VarName"/><xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][position()>1]">, <xsl:value-of select="VarName"/></xsl:for-each>);
   return GetModelFromRow(dr);
  }

  /// <summary>
  /// 根据逻辑主键检索数据实体
  /// </summary>
  /// <param name="DbEngine">数据引擎</param>
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true']">
  /// <param>
    <xsl:attribute name="name">
     <xsl:value-of select="VarName"/>
    </xsl:attribute>
    <xsl:value-of select="Mark"/>
   </param>
  </xsl:for-each>
  /// <returns>检索结果,数据实体</returns>
  internal <xsl:value-of select="TabelModel/ModelName"/>Model Get<xsl:value-of select="TabelModel/ModelName"/>ByLogicKey(SQLHelper DbEngine<xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true']">, <xsl:value-of select="VarType"/><xsl:text> </xsl:text><xsl:value-of select="VarName"/></xsl:for-each>)
  {
   DataRow dr = GetRowByLogicKey(DbEngine<xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true']">, <xsl:value-of select="VarName"/></xsl:for-each>);
   return GetModelFromRow(dr);
  }

  </xsl:if>
  
  /// <summary>
  /// 根据条件检索数据实体
  /// </summary>
  /// <param name="strWhere">条件</param>
  /// <returns>检索结果,数据实体</returns>
  public <xsl:value-of select="TabelModel/ModelName"/>Model Get<xsl:value-of select="TabelModel/ModelName"/>ByParam(string strWhere)
  {
   DataRow dr = GetRowByParam(strWhere);
   return GetModelFromRow(dr);
  }

  /// <summary>
  /// 根据条件检索数据实体
  /// </summary>
  /// <param name="DbEngine">数据引擎</param>
  /// <param name="strWhere">条件</param>
  /// <returns>检索结果,数据实体</returns>
  internal <xsl:value-of select="TabelModel/ModelName"/>Model Get<xsl:value-of select="TabelModel/ModelName"/>ByParam(SQLHelper DbEngine, string strWhere)
  {
   DataRow dr = GetRowByParam(DbEngine, strWhere);
   return GetModelFromRow(dr);
  }

  <xsl:if test="TabelModel/HasPK = 'true'">
  /// <summary>
  /// 根据主键检索DataRow
  /// </summary>
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true']">
  /// <param>
    <xsl:attribute name="name">
     <xsl:value-of select="VarName"/>
    </xsl:attribute>
    <xsl:value-of select="Mark"/>
   </param>
  </xsl:for-each>
  /// <returns>检索结果,DataRow</returns>
  public DataRow GetRowByPK(<xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/VarType"/><xsl:text> </xsl:text><xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/VarName"/><xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true'][position()>1]">, <xsl:value-of select="VarType"/><xsl:text> </xsl:text><xsl:value-of select="VarName"/></xsl:for-each>)
  {
   using (SQLHelper DbEngine = new SQLHelper())
   {
    return GetRowByPK(DbEngine<xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true']">, <xsl:value-of select="VarName"/></xsl:for-each>);
   }
  }

  /// <summary>
  /// 根据主键检索DataRow
  /// </summary>
  /// <param name="DbEngine">数据引擎</param>
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true']">
  /// <param>
    <xsl:attribute name="name">
     <xsl:value-of select="VarName"/>
    </xsl:attribute>
    <xsl:value-of select="Mark"/>
   </param>
  </xsl:for-each>
  /// <returns>检索结果,DataRow</returns>
  internal DataRow GetRowByPK(SQLHelper DbEngine<xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true']">, <xsl:value-of select="VarType"/><xsl:text> </xsl:text><xsl:value-of select="VarName"/></xsl:for-each>)
  {
   StringBuilder strSQL = new StringBuilder();
   strSQL.Append(" SELECT * FROM <xsl:value-of select="TabelModel/TableName"/> ");
   strSQL.Append(" WHERE ");
   strSQL.Append(" <xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/DbFieldName"/> = @<xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/DbFieldName"/> ");
   <xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true'][position() > 1]">strSQL.Append(" <xsl:value-of select="DbFieldName"/> = @<xsl:value-of select="DbFieldName"/> ");</xsl:for-each>

   SqlParameter[] parameters =
   {
    new SqlParameter("@<xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/DbFieldName"/>", <xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/SqlParamType"/>, <xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/Length"/>)
    <xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true'][position() > 1]">
    , new SqlParameter("@<xsl:value-of select="DbFieldName"/>", <xsl:value-of select="SqlParamType"/>, <xsl:value-of select="Length"/>)
    </xsl:for-each>
   };
   
   <xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true']">parameters[<xsl:value-of select="position()-1"/>].Value = <xsl:value-of select="VarName"/>;</xsl:for-each>

   return DbEngine.ExecDataRow(strSQL.ToString(), parameters);
  }
  </xsl:if>
  
  
  <xsl:if test="TabelModel/HasLogicKey = 'true'">
  /// <summary>
  /// 根据逻辑主键检索DataRow
  /// </summary>
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true']">
  /// <param>
    <xsl:attribute name="name">
     <xsl:value-of select="VarName"/>
    </xsl:attribute>
    <xsl:value-of select="Mark"/>
   </param>
  </xsl:for-each>
  /// <returns>检索结果,DataRow</returns>
  public DataRow GetRowByLogicKey(<xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/VarType"/><xsl:text> </xsl:text><xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/VarName"/><xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][position()>1]">, <xsl:value-of select="VarType"/><xsl:text> </xsl:text><xsl:value-of select="VarName"/></xsl:for-each>)
  {
   using (SQLHelper DbEngine = new SQLHelper())
   {
    return GetRowByLogicKey(DbEngine<xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true']">, <xsl:value-of select="VarName"/></xsl:for-each>);
   }
  }

  /// <summary>
  /// 根据逻辑主键检索DataRow
  /// </summary>
  /// <param name="DbEngine">数据引擎</param>
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true']">
  /// <param>
    <xsl:attribute name="name">
     <xsl:value-of select="VarName"/>
    </xsl:attribute>
    <xsl:value-of select="Mark"/>
   </param>
  </xsl:for-each>
  /// <returns>检索结果,DataRow</returns>
  internal DataRow GetRowByLogicKey(SQLHelper DbEngine<xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true']">, <xsl:value-of select="VarType"/><xsl:text> </xsl:text><xsl:value-of select="VarName"/></xsl:for-each>)
  {
   StringBuilder strSQL = new StringBuilder();
   strSQL.Append(" SELECT * FROM <xsl:value-of select="TabelModel/TableName"/> ");
   strSQL.Append(" WHERE ");
   strSQL.Append(" <xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/DbFieldName"/> = @<xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/DbFieldName"/> ");
   <xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][position() > 1]">strSQL.Append(" <xsl:value-of select="DbFieldName"/> = @<xsl:value-of select="DbFieldName"/> ");</xsl:for-each>

   SqlParameter[] parameters =
   {
    new SqlParameter("@<xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/DbFieldName"/>", <xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/SqlParamType"/>, <xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/Length"/>)
    <xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][position() > 1]">
    , new SqlParameter("@<xsl:value-of select="DbFieldName"/>", <xsl:value-of select="SqlParamType"/>, <xsl:value-of select="Length"/>)
    </xsl:for-each>
   };
   
   <xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true']">parameters[<xsl:value-of select="position()-1"/>].Value = <xsl:value-of select="VarName"/>;</xsl:for-each>

   return DbEngine.ExecDataRow(strSQL.ToString(), parameters);
  }
  </xsl:if>

  /// <summary>
  /// 根据条件获取数据列表(DataRow)
  /// </summary>
  /// <param name="strWhere">条件</param>
  /// <returns>DataTable</returns>
  public DataRow GetRowByParam(string strWhere)
  {
   using (SQLHelper DbEngine = new SQLHelper())
   {
    return GetRowByParam(DbEngine, strWhere);
   }
  }

  /// <summary>
  /// 根据条件获取数据(DataRow)
  /// </summary>
  /// <param name="DbEngine">数据引擎</param>
  /// <param name="strWhere">条件</param>
  /// <returns>DataTable</returns>
  internal DataRow GetRowByParam(SQLHelper DbEngine, string strWhere)
  {
   StringBuilder strSQL = new StringBuilder();
   strSQL.Append(" SELECT * FROM <xsl:value-of select="TabelModel/TableName"/> ");
   if (strWhere.Length <xsl:text disable-output-escaping="yes">&gt;</xsl:text> 0)
   {
    strSQL.Append(" WHERE " + strWhere);
   }

   return DbEngine.ExecDataRow(strSQL.ToString());
  }


  /// <summary>
  /// 获取所有数据列表
  /// </summary>
  /// <returns>数据实体列表</returns>
  public List<xsl:text disable-output-escaping="yes">&lt;</xsl:text><xsl:value-of select="TabelModel/ModelName"/>Model<xsl:text disable-output-escaping="yes">&gt;</xsl:text> Get<xsl:value-of select="TabelModel/ModelName"/>List()
  {
   return Get<xsl:value-of select="TabelModel/ModelName"/>ListByParam("");
  }

  /// <summary>
  /// 获取所有数据列表
  /// </summary>
  /// <param name="DbEngine">数据引擎</param>
  /// <returns>数据实体列表</returns>
  internal List<xsl:text disable-output-escaping="yes">&lt;</xsl:text><xsl:value-of select="TabelModel/ModelName"/>Model<xsl:text disable-output-escaping="yes">&gt;</xsl:text> Get<xsl:value-of select="TabelModel/ModelName"/>List(SQLHelper DbEngine)
  {
   return Get<xsl:value-of select="TabelModel/ModelName"/>ListByParam(DbEngine, "");
  }

  /// <summary>
  /// 根据条件获取数据列表
  /// </summary>
  /// <param name="strWhere">条件</param>
  /// <returns>数据实体列表</returns>
  public List<xsl:text disable-output-escaping="yes">&lt;</xsl:text><xsl:value-of select="TabelModel/ModelName"/>Model<xsl:text disable-output-escaping="yes">&gt;</xsl:text> Get<xsl:value-of select="TabelModel/ModelName"/>ListByParam(string strWhere)
  {
   DataTable dt = null;

   using (SQLHelper DbEngine = new SQLHelper())
   {
    dt = GetTableByParam(DbEngine, strWhere);
   }

   return GetListFromDataTable(dt);
  }

  /// <summary>
  /// 根据条件获取数据列表
  /// </summary>
  /// <param name="DbEngine">数据引擎</param>
  /// <param name="strWhere">条件</param>
  /// <returns>数据实体列表</returns>
  internal List<xsl:text disable-output-escaping="yes">&lt;</xsl:text><xsl:value-of select="TabelModel/ModelName"/>Model<xsl:text disable-output-escaping="yes">&gt;</xsl:text> Get<xsl:value-of select="TabelModel/ModelName"/>ListByParam(SQLHelper DbEngine, string strWhere)
  {
   DataTable dt = GetTableByParam(DbEngine, strWhere);

   return GetListFromDataTable(dt);
  }

  /// <summary>
  /// 根据条件获取数据列表(DataTable)
  /// </summary>
  /// <param name="strWhere">条件</param>
  /// <returns>DataTable</returns>
  public DataTable GetTableByParam(string strWhere)
  {
   using (SQLHelper DbEngine = new SQLHelper())
   {
    return GetTableByParam(DbEngine, strWhere);
   }
  }

  /// <summary>
  /// 根据条件获取数据列表(DataTable)
  /// </summary>
  /// <param name="DbEngine">数据引擎</param>
  /// <param name="strWhere">条件</param>
  /// <returns>DataTable</returns>
  internal DataTable GetTableByParam(SQLHelper DbEngine, string strWhere)
  {
   StringBuilder strSQL = new StringBuilder();
   strSQL.Append(" SELECT * FROM <xsl:value-of select="TabelModel/TableName"/> ");
   if (strWhere.Length <xsl:text disable-output-escaping="yes">&gt;</xsl:text> 0)
   {
    strSQL.Append(" WHERE " + strWhere);
   }

   return DbEngine.ExecDataTable(strSQL.ToString());
  }


  #endregion

  <xsl:if test="TabelModel/IsView = 'false'">
  #region 数据增/删/改

  /// <summary>
  /// 新增
  ///</summary>
  /// <param name="model">数据实体</param>
  /// <returns></returns>
  public int Add<xsl:value-of select="TabelModel/ModelName"/>(<xsl:value-of select="TabelModel/ModelName"/>Model model)
  {
   using (SQLHelper DbEngine = new SQLHelper())
   {
    return Add<xsl:value-of select="TabelModel/ModelName"/>(DbEngine, model);
   }
  }


  /// <summary>
  /// 新增
  /// </summary>
  /// <param name="DbEngine">指定数据库引擎</param>
  /// <param name="model">数据实体</param>
  /// <returns></returns>
  internal int Add<xsl:value-of select="TabelModel/ModelName"/>(SQLHelper DbEngine, <xsl:value-of select="TabelModel/ModelName"/>Model model)
  {
   StringBuilder strSQL = new StringBuilder();
   strSQL.Append("INSERT INTO <xsl:value-of select="TabelModel/TableName"/>(");
   strSQL.Append("<xsl:for-each select="TabelModel/Fields/FieldModel[IsIdentity = 'false'][position() != last()]"><xsl:value-of select="DbFieldName"/>,</xsl:for-each><xsl:value-of select="TabelModel/Fields/FieldModel[IsIdentity = 'false'][last()]/DbFieldName"></xsl:value-of>)");
   strSQL.Append("VALUES(");
   strSQL.Append("<xsl:for-each select="TabelModel/Fields/FieldModel[IsIdentity = 'false'][position() != last()]">@<xsl:value-of select="DbFieldName"/>,</xsl:for-each>@<xsl:value-of select="TabelModel/Fields/FieldModel[IsIdentity = 'false'][last()]/DbFieldName"></xsl:value-of>);SELECT @@IDENTITY");
   SqlParameter[] parameters =
   {
    new SqlParameter("@<xsl:value-of select="TabelModel/Fields/FieldModel[IsIdentity = 'false'][1]/DbFieldName"/>", <xsl:value-of select="TabelModel/Fields/FieldModel[IsIdentity = 'false'][1]/SqlParamType"/>, <xsl:value-of select="TabelModel/Fields/FieldModel[IsIdentity = 'false'][1]/Length"/>)<xsl:for-each select="TabelModel/Fields/FieldModel[IsIdentity = 'false'][position()>1]">
    ,new SqlParameter("@<xsl:value-of select="DbFieldName"/>", <xsl:value-of select="SqlParamType"/>, <xsl:value-of select="Length"/>)</xsl:for-each>
   };
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsIdentity = 'false']">
   parameters[<xsl:value-of select="position()-1"/>].Value = model.<xsl:value-of select="VarName"/>;<xsl:choose>
    <xsl:when test="VarType = 'DateTime' and AllowNull = 'true'">
   if (model.<xsl:value-of select="VarName"/> == DateTime.MinValue)
   {
    parameters[<xsl:value-of select="position()-1"/>].Value = DBNull.Value;
   }</xsl:when>
   </xsl:choose>
  </xsl:for-each>

   return Convert.ToInt32(DbEngine.ExecuteScalar(strSQL.ToString(), parameters));

   }

   <xsl:if test="TabelModel/HasPK = 'true'">
  /// <summary>
  /// 编辑
  /// </summary>
  /// <param name="model">数据实体</param>
  /// <returns></returns>
  public int Update<xsl:value-of select="TabelModel/ModelName"/>(<xsl:value-of select="TabelModel/ModelName"/>Model model)
  {
   using (SQLHelper DbEngine = new SQLHelper())
   {
    return Update<xsl:value-of select="TabelModel/ModelName"/>(DbEngine, model);
   }
  }

  /// <summary>
  /// 编辑
  /// </summary>
  /// <param name="DbEngine">指定数据库引擎</param>
  /// <param name="model">数据实体</param>
  /// <returns></returns>
  internal int Update<xsl:value-of select="TabelModel/ModelName"/>(SQLHelper DbEngine, <xsl:value-of select="TabelModel/ModelName"/>Model model)
  {
   StringBuilder strSQL = new StringBuilder();
   strSQL.Append("UPDATE <xsl:value-of select="TabelModel/TableName"/> SET ");
   <xsl:for-each select="TabelModel/Fields/FieldModel[IsIdentity = 'false' and IsPK = 'false'][position() != last()]">strSQL.Append("<xsl:value-of select="DbFieldName"/> = @<xsl:value-of select="DbFieldName"/>,");
   </xsl:for-each>strSQL.Append("<xsl:value-of select="TabelModel/Fields/FieldModel[IsIdentity = 'false' and IsPK = 'false'][last()]/DbFieldName"/> = @<xsl:value-of select="TabelModel/Fields/FieldModel[IsIdentity = 'false' and IsPK = 'false'][last()]/DbFieldName"/>");
   strSQL.Append(" WHERE ");
   strSQL.Append("<xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/DbFieldName"/> = @<xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/DbFieldName"/> ");
   <xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true'][position() > 1]">strSQL.Append(" AND <xsl:value-of select="DbFieldName"/> = @<xsl:value-of select="DbFieldName"/> ");
   </xsl:for-each>

   SqlParameter[] parameters =
   {
    new SqlParameter("@<xsl:value-of select="TabelModel/Fields/FieldModel[IsIdentity = 'false' or IsPK = 'true'][1]/DbFieldName"/>", <xsl:value-of select="TabelModel/Fields/FieldModel[IsIdentity = 'false' or IsPK = 'true'][1]/SqlParamType"/>, <xsl:value-of select="TabelModel/Fields/FieldModel[IsIdentity = 'false' or IsPK = 'true'][1]/Length"/>)<xsl:for-each select="TabelModel/Fields/FieldModel[IsIdentity = 'false' or IsPK = 'true'][position()>1]">
    ,new SqlParameter("@<xsl:value-of select="DbFieldName"/>", <xsl:value-of select="SqlParamType"/>, <xsl:value-of select="Length"/>)</xsl:for-each>
   };
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsIdentity = 'false' or IsPK = 'true']">
   parameters[<xsl:value-of select="position()-1"/>].Value = model.<xsl:value-of select="VarName"/>;<xsl:choose>
    <xsl:when test="VarType = 'DateTime' and AllowNull = 'true'">
   if (model.<xsl:value-of select="VarName"/> == DateTime.MinValue)
   {
    parameters[<xsl:value-of select="position()-1"/>].Value = DBNull.Value;
   }
    </xsl:when>
   </xsl:choose>
  </xsl:for-each>

   return DbEngine.ExecNonQuery(strSQL.ToString(), parameters);
  }


  /// <summary>
  /// 删除数据实体
  /// </summary>
  /// <param name="model">数据实体</param>
  /// <returns></returns>
  public int Delete<xsl:value-of select="TabelModel/ModelName"/>(<xsl:value-of select="TabelModel/ModelName"/>Model model)
  {
   return DeleteWithPK(model.<xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/VarName"/><xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true'][position()>1]">, model.<xsl:value-of select="VarName"/></xsl:for-each>);
  }

  /// <summary>
  /// 删除数据实体
  /// </summary>
  /// <param name="model">数据实体</param>
  /// <returns></returns>
  internal int Delete<xsl:value-of select="TabelModel/ModelName"/>(SQLHelper DbEngine, <xsl:value-of select="TabelModel/ModelName"/>Model model)
  {
   return DeleteWithPK(DbEngine<xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true']">, model.<xsl:value-of select="VarName"/></xsl:for-each>);
  }

  /// <summary>
  /// 根据主键删除
  /// </summary>
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true']">
  /// <param>
    <xsl:attribute name="name">
     <xsl:value-of select="VarName"/>
    </xsl:attribute>
    <xsl:value-of select="VarName"/>
   </param>
  </xsl:for-each>
  /// <returns></returns>
  public int DeleteWithPK(<xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/VarType"/><xsl:text> </xsl:text><xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/VarName"/><xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true'][position()>1]">, <xsl:value-of select="VarType"/><xsl:text> </xsl:text><xsl:value-of select="VarName"/></xsl:for-each>)
  {
   using (SQLHelper DbEngine = new SQLHelper())
   {
    return DeleteWithPK(DbEngine<xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true']">, <xsl:value-of select="VarName"/></xsl:for-each>);
   }
  }

  /// <summary>
  /// 根据主键删除
  /// </summary>
  /// <param name="DbEngine">指定数据库引擎</param>
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true']">
  /// <param>
    <xsl:attribute name="name">
     <xsl:value-of select="VarName"/>
    </xsl:attribute>
    <xsl:value-of select="VarName"/>
   </param>
  </xsl:for-each>
  /// <returns>删除行数</returns>
  internal int DeleteWithPK(SQLHelper DbEngine<xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true']">, <xsl:value-of select="VarType"/><xsl:text> </xsl:text><xsl:value-of select="VarName"/></xsl:for-each>)
  {
   StringBuilder strSQL = new StringBuilder();
   strSQL.Append(" DELETE FROM <xsl:value-of select="TabelModel/TableName"/> ");
   strSQL.Append(" WHERE <xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/DbFieldName"/> = @<xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/DbFieldName"/> ");
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true'][position()>1]">
   strSQL.Append(" AND <xsl:value-of select="DbFieldName"/> = @<xsl:value-of select="DbFieldName"/> ");
  </xsl:for-each>
   SqlParameter[] parameters =
   {
    new SqlParameter("@<xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/DbFieldName"/>", <xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/SqlParamType"/>, <xsl:value-of select="TabelModel/Fields/FieldModel[IsPK = 'true'][1]/Length"/>)
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true'][position()>1]">
    ,new SqlParameter("@<xsl:value-of select="DbFieldName"/>", <xsl:value-of select="SqlParamType"/>, <xsl:value-of select="Length"/>)
  </xsl:for-each>
   };
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsPK = 'true']">
   parameters[<xsl:value-of select="position()-1"/>].Value = <xsl:value-of select="VarName"/>;
  </xsl:for-each>

   return DbEngine.ExecNonQuery(strSQL.ToString(), parameters);
  }

  </xsl:if>
   
  <xsl:if test="TabelModel/HasLogicKey = 'true'">
  /// <summary>
  /// 编辑(根据逻辑主键)
  /// </summary>
  /// <param name="model">数据实体</param>
  /// <returns></returns>
  public int Update<xsl:value-of select="TabelModel/ModelName"/>WithLogicKey(<xsl:value-of select="TabelModel/ModelName"/>Model model)
  {
   using (SQLHelper DbEngine = new SQLHelper())
   {
    return Update<xsl:value-of select="TabelModel/ModelName"/>WithLogicKey(DbEngine, model);
   }
  }

  /// <summary>
  /// 编辑(根据逻辑主键)
  /// </summary>
  /// <param name="DbEngine">指定数据库引擎</param>
  /// <param name="model">数据实体</param>
  /// <returns></returns>
  internal int Update<xsl:value-of select="TabelModel/ModelName"/>WithLogicKey(SQLHelper DbEngine, <xsl:value-of select="TabelModel/ModelName"/>Model model)
  {
   StringBuilder strSQL = new StringBuilder();
   strSQL.Append("UPDATE <xsl:value-of select="TabelModel/TableName"/> SET ");
   <xsl:for-each select="TabelModel/Fields/FieldModel[IsIdentity = 'false' and IsLogicKey = 'false'][position() != last()]">strSQL.Append("<xsl:value-of select="DbFieldName"/> = @<xsl:value-of select="DbFieldName"/>,");
   </xsl:for-each>strSQL.Append("<xsl:value-of select="TabelModel/Fields/FieldModel[IsIdentity = 'false' and IsLogicKey = 'false'][last()]/DbFieldName"/> = @<xsl:value-of select="TabelModel/Fields/FieldModel[IsIdentity = 'false' and IsLogicKey = 'false'][last()]/DbFieldName"/>,");
   strSQL.Append(" WHERE ");
   strSQL.Append("<xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/DbFieldName"/> = @<xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/DbFieldName"/> ");
   <xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][position() > 1]">strSQL.Append(" AND <xsl:value-of select="DbFieldName"/> = @<xsl:value-of select="DbFieldName"/> ");
   </xsl:for-each>

   SqlParameter[] parameters =
   {
    new SqlParameter("@<xsl:value-of select="TabelModel/Fields/FieldModel[IsIdentity = 'false' or IsLogicKey = 'true'][1]/DbFieldName"/>", <xsl:value-of select="TabelModel/Fields/FieldModel[IsIdentity = 'false' or IsLogicKey = 'true'][1]/SqlParamType"/>, <xsl:value-of select="TabelModel/Fields/FieldModel[IsIdentity = 'false' or IsLogicKey = 'true'][1]/Length"/>)<xsl:for-each select="TabelModel/Fields/FieldModel[IsIdentity = 'false' or IsLogicKey = 'true'][position()>1]">
    ,new SqlParameter("@<xsl:value-of select="DbFieldName"/>", <xsl:value-of select="SqlParamType"/>, <xsl:value-of select="Length"/>)</xsl:for-each>
   };
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsIdentity = 'false' or IsLogicKey = 'true']">
   parameters[<xsl:value-of select="position()-1"/>].Value = model.<xsl:value-of select="VarName"/>;<xsl:choose>
    <xsl:when test="VarType = 'DateTime' and AllowNull = 'true'">
   if (model.<xsl:value-of select="VarName"/> == DateTime.MinValue)
   {
    parameters[<xsl:value-of select="position()-1"/>].Value = DBNull.Value;
   }
    </xsl:when>
   </xsl:choose>
  </xsl:for-each>

   return DbEngine.ExecNonQuery(strSQL.ToString(), parameters);
  }


  /// <summary>
  /// 删除数据实体
  /// </summary>
  /// <param name="model">数据实体</param>
  /// <returns></returns>
  public int Delete<xsl:value-of select="TabelModel/ModelName"/>WithLogicKey(<xsl:value-of select="TabelModel/ModelName"/>Model model)
  {
   return DeleteWithLogicKey(model.<xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/VarName"/><xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][position()>1]">, model.<xsl:value-of select="VarName"/></xsl:for-each>);
  }

  /// <summary>
  /// 删除数据实体
  /// </summary>
  /// <param name="model">数据实体</param>
  /// <returns></returns>
  internal int Delete<xsl:value-of select="TabelModel/ModelName"/>WithLogicKey(SQLHelper DbEngine, <xsl:value-of select="TabelModel/ModelName"/>Model model)
  {
   return DeleteWithLogicKey(DbEngine<xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true']">, model.<xsl:value-of select="VarName"/></xsl:for-each>);
  }

  /// <summary>
  /// 根据逻辑主键删除
  /// </summary>
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true']">
  /// <param>
    <xsl:attribute name="name">
     <xsl:value-of select="VarName"/>
    </xsl:attribute>
    <xsl:value-of select="VarName"/>
   </param>
  </xsl:for-each>
  /// <returns></returns>
  public int DeleteWithLogicKey(<xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/VarType"/><xsl:text> </xsl:text><xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/VarName"/><xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][position()>1]">, <xsl:value-of select="VarType"/><xsl:text> </xsl:text><xsl:value-of select="VarName"/></xsl:for-each>)
  {
   using (SQLHelper DbEngine = new SQLHelper())
   {
    return DeleteWithLogicKey(DbEngine<xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true']">, <xsl:value-of select="VarName"/></xsl:for-each>);
   }
  }

  /// <summary>
  /// 根据逻辑主键删除
  /// </summary>
  /// <param name="DbEngine">指定数据库引擎</param>
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true']">
  /// <param>
    <xsl:attribute name="name">
     <xsl:value-of select="VarName"/>
    </xsl:attribute>
    <xsl:value-of select="VarName"/>
   </param>
  </xsl:for-each>
  /// <returns>删除行数</returns>
  internal int DeleteWithLogicKey(SQLHelper DbEngine<xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true']">, <xsl:value-of select="VarType"/><xsl:text> </xsl:text><xsl:value-of select="VarName"/></xsl:for-each>)
  {
   StringBuilder strSQL = new StringBuilder();
   strSQL.Append(" DELETE FROM <xsl:value-of select="TabelModel/TableName"/> ");
   strSQL.Append(" WHERE <xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/DbFieldName"/> = @<xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/DbFieldName"/> ");
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][position()>1]">
   strSQL.Append(" AND <xsl:value-of select="DbFieldName"/> = @<xsl:value-of select="DbFieldName"/> ");
  </xsl:for-each>
   SqlParameter[] parameters =
   {
    new SqlParameter("@<xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/DbFieldName"/>", <xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/SqlParamType"/>, <xsl:value-of select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][1]/Length"/>)
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true'][position()>1]">
    ,new SqlParameter("@<xsl:value-of select="DbFieldName"/>", <xsl:value-of select="SqlParamType"/>, <xsl:value-of select="Length"/>)
  </xsl:for-each>
   };
  <xsl:for-each select="TabelModel/Fields/FieldModel[IsLogicKey = 'true']">
   parameters[<xsl:value-of select="position()-1"/>].Value = <xsl:value-of select="VarName"/>;
  </xsl:for-each>

   return DbEngine.ExecNonQuery(strSQL.ToString(), parameters);
  }

  </xsl:if>
  
  /// <summary>
  /// 根据条件删除
  /// </summary>
  /// <param name="strWhere">条件</param>
  /// <returns>删除条数</returns>
  public int DeleteByParam(string strWhere)
  {
   using (SQLHelper DbEngine = new SQLHelper())
   {
    return DeleteByParam(DbEngine, strWhere);
   }
  }

  /// <summary>
  /// 根据条件删除
  /// </summary>
  /// <param name="DbEngine">数据引擎</param>
  /// <param name="strWhere">条件</param>
  /// <returns>删除条数</returns>
  internal int DeleteByParam(SQLHelper DbEngine, string strWhere)
  {
   StringBuilder strSQL = new StringBuilder();
   strSQL.Append(" Delete FROM <xsl:value-of select="TabelModel/TableName"/> ");
   if (strWhere.Length <xsl:text disable-output-escaping="yes">&gt;</xsl:text> 0)
   {
    strSQL.Append(" WHERE " + strWhere);
   }

   return DbEngine.ExecNonQuery(strSQL.ToString());
  }
  
  #endregion  
  </xsl:if>
 }
}
 </xsl:template>
</xsl:stylesheet>




相关评论