- <%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="DALCode.cs" Inherits="Rakor.DALCode" Debug="False" ResponseEncoding="UTF-8" Description="Creates a simplistic entity object." %>
- <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="" %>
- <%@ Property Name="SolutionName" Type="String" Category="Context" Description="解决方案的名称" %>
- <%@ Property Name="ProjectName" Type="String" Category="Context" Description="项目名称" %>
- <%@ Property Name="PrimaryKey" Type="Int32" Category="Context" Description="主键序号" %>
- <%@ Property Name="UniqueKey" Type="Int32" Category="Context" Description="唯一列序号,用于检查是否唯一" %>
- <%@ Assembly Name="SchemaExplorer" %>
- <%@ Import Namespace="SchemaExplorer" %>
- /***************************************************************************************************************************************************
- *名称 :数据访问类。
- *作者 :曹鹏飞。
- *描述 :由CodeSmith根据数据库自动生成的数据访问类。
- *创建时间 : <%=DateTime.Now.ToString()%>
- ***************************************************************************************************************************************************/
- using System;
- using System.Collections.Generic;
- using System.Data;
- using <%=SolutionName%>.<%=ProjectName%>.Entity;
- using <%=SolutionName%>.<%=ProjectName%>.COMP;
- namespace <%=SolutionName%>.<%=ProjectName%>.DAL
- {
- public class <%=SourceTable.Name%>DAL
- {
- /// <summary>
- /// 检查数据是否存在
- /// </summary>
- /// <param name="ID"></param>
- /// <param name="str">要搜索的字符串</param>
- public bool Exist(int ID,string str)
- {
- 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%>");
- if (db.ExecuteScalar(cmd).ToString() == "0")
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// 检查数据是否存在
- /// </summary>
- /// <param name="str">要搜索的字符串</param>
- public bool Exist(string str)
- {
- DbCommand cmd = db.GetSqlStringCommand("SELECT COUNT(1) FROM <%=SourceTable.Name%> WHERE <%=SourceTable.Columns[UniqueKey].Name%>=@<%=SourceTable.Columns[UniqueKey].Name%>");
- if (db.ExecuteScalar(cmd).ToString() == "0")
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// 添加一条数据
- /// </summary>
- /// <param name="model"></param>
- public int Add(<%=SourceTable.Name%> model)
- {
- DbCommand cmd = db.GetStoredProcCommand("Proc_<%=SourceTable.Name%>_Add");
- <% for (int i = 1; i < SourceTable.Columns.Count; i++)
- { %>
- db.AddInParameter(cmd, "@<%=SourceTable.Columns[i].Name%>", DbType.<%=SourceTable.Columns[i].DataType.ToString().Replace("AnsiString","String")%>, model.<%=SourceTable.Columns[i].Name%>);
- <% } %>
- return int.Parse(db.ExecuteScalar(cmd).ToString());
- }
- /// <summary>
- /// 修改一条数据
- /// </summary>
- /// <param name="model"></param>
- public void Update(<%=SourceTable.Name%> model)
- {
- DbCommand cmd = db.GetStoredProcCommand("Proc_<%=SourceTable.Name%>_Update");
- <% for (int i = 0; i < SourceTable.Columns.Count; i++)
- { %>
- db.AddInParameter(cmd, "@<%=SourceTable.Columns[i].Name%>", DbType.<%=SourceTable.Columns[i].DataType.ToString().Replace("AnsiString","String")%>, model.<%=SourceTable.Columns[i].Name%>);
- <% } %>
- db.ExecuteNonQuery(cmd);
- }
- /// <summary>
- /// 获取一条数据
- /// </summary>
- /// <param name="ID"></param>
- public <%=SourceTable.Name%> GetModel(int ID)
- {
- DbCommand cmd = db.GetSqlStringCommand("SELECT * FROM <%=SourceTable.Name%> WHERE <%=SourceTable.Columns[PrimaryKey].Name%>=@<%=SourceTable.Columns[PrimaryKey].Name%>");
- db.AddInParameter(cmd, "@<%=SourceTable.Columns[PrimaryKey].Name%>", DbType.<%=SourceTable.Columns[PrimaryKey].DataType%>, ID);
- <%=SourceTable.Name%> model = null;
- IDataReader reader = db.ExecuteReader(cmd);
- if(reader.Read())
- {
- <% for (int i = 0; i < SourceTable.Columns.Count; i++)
- { %>
- model.<%=SourceTable.Columns[i].Name%>=<%=ConvertToType(SourceTable.Columns[i])%>;
- <% } %>
- }
- reader.Close();
- return model;
- }
- /// <summary>
- /// 删除一条数据
- /// </summary>
- /// <param name="ID"></param>
- public void Delete(Int32 ID)
- {
- DbCommand cmd = db.GetSqlStringCommand("DELETE <%=SourceTable.Name%> WHERE <%=SourceTable.Columns[PrimaryKey].Name%>=@<%=SourceTable.Columns[PrimaryKey].Name%>");
- db.AddInParameter(cmd, "@<%=SourceTable.Columns[PrimaryKey].Name%>", DbType.<%=SourceTable.Columns[PrimaryKey].DataType%>, ID);
- db.ExecuteNonQuery(cmd);
- }
- /// <summary>
- /// 分页获取信息
- /// </summary>
- /// <param name="PageSize">每页大小</param>
- /// <param name="PageIndex">当前页数</param>
- /// <param name="WhereSql">查询条件(不用加Where)</param>
- /// <param name="OrderSql">排序方式(不用加Order By)</param>
- /// <param name="Count">记录总数</param>
- /// <returns></returns>
- public List<<%=SourceTable.Name%>> GetDataByPager(int PageSize, int PageIndex, string WhereSql, string OrderSql, out int Count)
- {
- DbCommand cmd = db.GetStoredProcCommand("Proc_Pager");
- db.AddInParameter(cmd, "@tbname", DbType.String, "nView_News_Manager_Class");
- db.AddInParameter(cmd, "@FieldKey", DbType.String, "ID");
- db.AddInParameter(cmd, "@PageCurrent", DbType.Int32, PageIndex);
- db.AddInParameter(cmd, "@PageSize", DbType.Int32, PageSize);
- db.AddInParameter(cmd, "@FieldShow", DbType.String, "*");
- db.AddInParameter(cmd, "@FieldOrder", DbType.String, OrderSql);
- db.AddInParameter(cmd, "@Where", DbType.String, WhereSql);
- db.AddOutParameter(cmd, "@TotalCount", DbType.Int32, 0);
- IDataReader reader = db.ExecuteReader(cmd);
- List<<%=SourceTable.Name%>> list = new List<<%=SourceTable.Name%>>();
- while (reader.Read())
- {
- <% for (int i = 0; i < SourceTable.Columns.Count; i++)
- { %>
- model.<%=SourceTable.Columns[i].Name%>=<%=ConvertToType(SourceTable.Columns[i])%>;
- <% } %>
- }
- reader.Close();
- Count = Int32.Parse(cmd.Parameters["@TotalCount"].Value.ToString());
- return list;
- }
- }
- }
后台文件代码:
- using CodeSmith.Engine;
- using SchemaExplorer;
- using System;
- using System.Windows.Forms.Design;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Globalization;
- using System.Reflection;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.IO;
- using System.Xml;
- using System.Xml.Serialization;
- using System.Diagnostics;
- namespace Rakor
- {
- public class DALCode : CodeTemplate
- {
- ///<summary>
- ///将数据转换为对应的数据类型
- ///<param name="column">需要转换的列</param>
- ///</summary>
- public string ConvertToType(ColumnSchema column)
- {
- switch(column.SystemType.ToString())
- {
- case "System.String":
- return "reader[\""+column.Name+"\"].ToString()";
- case "System.Int32":
- return "Int32.Parse(reader[\""+column.Name+"\"].ToString())";
- case "System.Int64":
- return "Int64.Parse(reader[\""+column.Name+"\"].ToString())";
- case "System.DateTime":
- return "DateTime.Parse(reader[\""+column.Name+"\"].ToString())";
- default:
- return "";
- }
- }
- }
- }