博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

用CodeSmith生成基于企业库的数据访问类

Posted on 2009-08-05 00:29  linFen  阅读(640)  评论(0编辑  收藏  举报
  1. <%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="DALCode.cs" Inherits="Rakor.DALCode" Debug="False" ResponseEncoding="UTF-8" Description="Creates a simplistic entity object." %>  
  2. <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="" %>  
  3. <%@ Property Name="SolutionName" Type="String" Category="Context" Description="解决方案的名称" %>  
  4. <%@ Property Name="ProjectName" Type="String" Category="Context" Description="项目名称" %>  
  5. <%@ Property Name="PrimaryKey" Type="Int32" Category="Context" Description="主键序号" %>  
  6. <%@ Property Name="UniqueKey" Type="Int32" Category="Context" Description="唯一列序号,用于检查是否唯一" %>  
  7. <%@ Assembly Name="SchemaExplorer" %>  
  8. <%@ Import Namespace="SchemaExplorer" %>  
  9. /***************************************************************************************************************************************************  
  10. *名称      :数据访问类。  
  11. *作者      :曹鹏飞。  
  12. *描述      :由CodeSmith根据数据库自动生成的数据访问类。  
  13. *创建时间  : <%=DateTime.Now.ToString()%>  
  14. ***************************************************************************************************************************************************/ 
  15. using System;  
  16. using System.Collections.Generic;  
  17. using System.Data;  
  18. using <%=SolutionName%>.<%=ProjectName%>.Entity;  
  19. using <%=SolutionName%>.<%=ProjectName%>.COMP;  
  20.  
  21. namespace <%=SolutionName%>.<%=ProjectName%>.DAL  
  22. {  
  23.     public class <%=SourceTable.Name%>DAL  
  24.     {  
  25.         /// <summary>  
  26.         /// 检查数据是否存在  
  27.         /// </summary>  
  28.         /// <param name="ID"></param>  
  29.         /// <param name="str">要搜索的字符串</param>  
  30.         public bool Exist(int ID,string str)  
  31.         {  
  32.             DbCommand cmd = db.GetSqlStringCommand("SELECT COUNT(1) FROM <%=SourceTable.Name%> WHERE <%=SourceTable.Columns[PrimaryKey].Name%>=@<%=SourceTable.Columns[PrimaryKey].Name%> AND <%=SourceTable.Columns[UniqueKey].Name%>=@<%=SourceTable.Columns[UniqueKey].Name%>");  
  33.             if (db.ExecuteScalar(cmd).ToString() == "0")  
  34.             {  
  35.                 return true;  
  36.             }  
  37.             else 
  38.             {  
  39.                 return false;  
  40.             }  
  41.         }  
  42.         /// <summary>  
  43.         /// 检查数据是否存在  
  44.         /// </summary>  
  45.         /// <param name="str">要搜索的字符串</param>  
  46.         public bool Exist(string str)  
  47.         {  
  48.             DbCommand cmd = db.GetSqlStringCommand("SELECT COUNT(1) FROM <%=SourceTable.Name%> WHERE <%=SourceTable.Columns[UniqueKey].Name%>=@<%=SourceTable.Columns[UniqueKey].Name%>");  
  49.             if (db.ExecuteScalar(cmd).ToString() == "0")  
  50.             {  
  51.                 return true;  
  52.             }  
  53.             else 
  54.             {  
  55.                 return false;  
  56.             }  
  57.         }  
  58.         /// <summary>  
  59.         /// 添加一条数据  
  60.         /// </summary>  
  61.         /// <param name="model"></param>  
  62.         public int Add(<%=SourceTable.Name%> model)  
  63.         {  
  64.             DbCommand cmd = db.GetStoredProcCommand("Proc_<%=SourceTable.Name%>_Add");  
  65.             <% for (int i = 1; i < SourceTable.Columns.Count; i++)  
  66.             { %>  
  67.             db.AddInParameter(cmd, "@<%=SourceTable.Columns[i].Name%>", DbType.<%=SourceTable.Columns[i].DataType.ToString().Replace("AnsiString","String")%>, model.<%=SourceTable.Columns[i].Name%>);  
  68.             <% } %>  
  69.             return int.Parse(db.ExecuteScalar(cmd).ToString());  
  70.         }  
  71.         /// <summary>  
  72.         /// 修改一条数据  
  73.         /// </summary>  
  74.         /// <param name="model"></param>  
  75.         public void Update(<%=SourceTable.Name%> model)  
  76.         {  
  77.             DbCommand cmd = db.GetStoredProcCommand("Proc_<%=SourceTable.Name%>_Update");  
  78.             <% for (int i = 0; i < SourceTable.Columns.Count; i++)  
  79.             { %>  
  80.             db.AddInParameter(cmd, "@<%=SourceTable.Columns[i].Name%>", DbType.<%=SourceTable.Columns[i].DataType.ToString().Replace("AnsiString","String")%>, model.<%=SourceTable.Columns[i].Name%>);  
  81.             <% } %>  
  82.             db.ExecuteNonQuery(cmd);  
  83.         }  
  84.         /// <summary>  
  85.         /// 获取一条数据  
  86.         /// </summary>  
  87.         /// <param name="ID"></param>  
  88.         public <%=SourceTable.Name%> GetModel(int ID)  
  89.         {  
  90.             DbCommand cmd = db.GetSqlStringCommand("SELECT * FROM <%=SourceTable.Name%> WHERE <%=SourceTable.Columns[PrimaryKey].Name%>=@<%=SourceTable.Columns[PrimaryKey].Name%>");  
  91.             db.AddInParameter(cmd, "@<%=SourceTable.Columns[PrimaryKey].Name%>", DbType.<%=SourceTable.Columns[PrimaryKey].DataType%>, ID);  
  92.             <%=SourceTable.Name%> model = null;  
  93.             IDataReader reader = db.ExecuteReader(cmd);  
  94.             if(reader.Read())  
  95.             {  
  96.                 <% for (int i = 0; i < SourceTable.Columns.Count; i++)  
  97.                 { %>  
  98.                 model.<%=SourceTable.Columns[i].Name%>=<%=ConvertToType(SourceTable.Columns[i])%>;  
  99.                 <% } %>  
  100.             }  
  101.             reader.Close();  
  102.             return model;  
  103.         }  
  104.         /// <summary>  
  105.         /// 删除一条数据  
  106.         /// </summary>  
  107.         /// <param name="ID"></param>  
  108.         public void Delete(Int32 ID)  
  109.         {  
  110.             DbCommand cmd = db.GetSqlStringCommand("DELETE <%=SourceTable.Name%> WHERE <%=SourceTable.Columns[PrimaryKey].Name%>=@<%=SourceTable.Columns[PrimaryKey].Name%>");  
  111.             db.AddInParameter(cmd, "@<%=SourceTable.Columns[PrimaryKey].Name%>", DbType.<%=SourceTable.Columns[PrimaryKey].DataType%>, ID);  
  112.             db.ExecuteNonQuery(cmd);  
  113.         }  
  114.         /// <summary>  
  115.         /// 分页获取信息  
  116.         /// </summary>  
  117.         /// <param name="PageSize">每页大小</param>  
  118.         /// <param name="PageIndex">当前页数</param>  
  119.         /// <param name="WhereSql">查询条件(不用加Where)</param>  
  120.         /// <param name="OrderSql">排序方式(不用加Order By)</param>  
  121.         /// <param name="Count">记录总数</param>  
  122.         /// <returns></returns>  
  123.         public List<<%=SourceTable.Name%>> GetDataByPager(int PageSize, int PageIndex, string WhereSql, string OrderSql, out int Count)  
  124.         {  
  125.             DbCommand cmd = db.GetStoredProcCommand("Proc_Pager");  
  126.             db.AddInParameter(cmd, "@tbname", DbType.String, "nView_News_Manager_Class");  
  127.             db.AddInParameter(cmd, "@FieldKey", DbType.String, "ID");  
  128.             db.AddInParameter(cmd, "@PageCurrent", DbType.Int32, PageIndex);  
  129.             db.AddInParameter(cmd, "@PageSize", DbType.Int32, PageSize);  
  130.             db.AddInParameter(cmd, "@FieldShow", DbType.String, "*");  
  131.             db.AddInParameter(cmd, "@FieldOrder", DbType.String, OrderSql);  
  132.             db.AddInParameter(cmd, "@Where", DbType.String, WhereSql);  
  133.             db.AddOutParameter(cmd, "@TotalCount", DbType.Int32, 0);  
  134.             IDataReader reader = db.ExecuteReader(cmd);  
  135.             List<<%=SourceTable.Name%>> list = new List<<%=SourceTable.Name%>>();  
  136.             while (reader.Read())  
  137.             {  
  138.                 <% for (int i = 0; i < SourceTable.Columns.Count; i++)  
  139.                 { %>  
  140.                 model.<%=SourceTable.Columns[i].Name%>=<%=ConvertToType(SourceTable.Columns[i])%>;  
  141.                 <% } %>  
  142.             }  
  143.             reader.Close();  
  144.             Count = Int32.Parse(cmd.Parameters["@TotalCount"].Value.ToString());  
  145.             return list;  
  146.         }  
  147.     }  

后台文件代码:

  1. using CodeSmith.Engine;  
  2. using SchemaExplorer;  
  3. using System;  
  4. using System.Windows.Forms.Design;  
  5. using System.Collections;  
  6. using System.Collections.Generic;  
  7. using System.ComponentModel;  
  8. using System.Data;  
  9. using System.Globalization;  
  10. using System.Reflection;  
  11. using System.Text;  
  12. using System.Text.RegularExpressions;  
  13. using System.IO;  
  14. using System.Xml;  
  15. using System.Xml.Serialization;  
  16. using System.Diagnostics;  
  17.  
  18. namespace Rakor  
  19. {  
  20.     public class DALCode : CodeTemplate  
  21.     {  
  22.         ///<summary>  
  23.         ///将数据转换为对应的数据类型  
  24.         ///<param name="column">需要转换的列</param>  
  25.         ///</summary>  
  26.         public string ConvertToType(ColumnSchema column)  
  27.         {  
  28.             switch(column.SystemType.ToString())  
  29.             {  
  30.                 case "System.String":  
  31.                     return "reader[\""+column.Name+"\"].ToString()";  
  32.                 case "System.Int32":  
  33.                     return "Int32.Parse(reader[\""+column.Name+"\"].ToString())";  
  34.                 case "System.Int64":  
  35.                     return "Int64.Parse(reader[\""+column.Name+"\"].ToString())";  
  36.                 case "System.DateTime":  
  37.                     return "DateTime.Parse(reader[\""+column.Name+"\"].ToString())";  
  38.                 default:  
  39.                     return "";  
  40.             }  
  41.         }  
  42.     }