ASP.NET三层架构源码(CodeSmith版)之十一:View-SQLServerDAL层
动软代码生成器生成的ASP.NET三层架构代码比较规范,是学习ASP.NET的好例子
此三层架构改造自动软的工厂模式模板,使用CodeSmith进行重写,以方便大家修改模板文件
以下是针对视图SQLServerDAL层的源码:
<%@ CodeTemplate Inherits="CodeTemplate" language="C#" TargetLanguage="Text" Description="NetTiers main template."Debug="True" ResponseEncoding="UTF-8"%>
<%-- 加载访问数据库的组件SchemaExplorer,并声明其使用的命名空间 --%>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Text" %>
<%@ Assembly Name="System.Data" %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Import Namespace="CodeSmith.BaseTemplates" %>
<%@ Assembly Name="CodeSmith.CustomProperties" %>
<%@ Import Namespace="CodeSmith.CustomProperties" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<%@ Property Name="Namespace" Type="String" Category="Context" Description="NameSpace"%>
<%@ Property Name="Author" Type="String" Category="Context" Description="Author" Default="chenr"%>
<%@ Property Name="ViewPrefix" Type="System.String" Default="T" Category="Context" Description="The prefix to remove from table names" %>
<%@ Property Name="ViewName" Type="ViewSchema" DeepLoad="True" Optional="False" Category="Context" Description="" %>
/*------------------------------------------------
// File Name:<%=ClearPrefix(ViewName.Name) %>.cs
// File Description:<%=ClearPrefix(ViewName.Name) %> SQL Server DataBase Access
// Author:<%=Author%>
// Create Time:<%= DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")%>
//------------------------------------------------*/
using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using <%=Namespace%>.DBUtility;
using <%=Namespace%>.IDAL;
using <%=Namespace%>.Model;
namespace <%=Namespace%>.SQLServerDAL
{
///
/// <%=ClearPrefix(ViewName.Name) %> DAL
///
public partial class <%=ClearPrefix(ViewName.Name) %>: I<%=ClearPrefix(ViewName.Name) %>
{
#region Method
///
/// 得到最大ID
///
public int GetMaxId(string colName)
{
return DbHelperSQL.GetMaxID(colName, "<%=ClearPrefix(ViewName.Name) %>");
//DbHelperSQL.RunProcedure("tTest_GetMaxId","",out rowsAffected);
//return rowsAffected;
}
///
/// 是否存在该记录
///
public bool Exists()
{
return true;
}
///
/// 获得数据列表
///
public DataSet GetList(string strWhere)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("select <%= GetAllRows(ViewName)%>");
strSql.Append(" FROM <%=ClearPrefix(ViewName.Name) %> ");
if(strWhere.Trim()!="")
{
strSql.Append(" where "+strWhere);
}
return DbHelperSQL.Query(strSql.ToString());
}
///
/// 获得前几行数据
///
public DataSet GetList(int Top,string strWhere,string filedOrder)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("select ");
if(Top>0)
{
strSql.Append(" top "+Top.ToString());
}
strSql.Append(" <%= GetAllRows(ViewName)%> ");
strSql.Append(" FROM <%=ClearPrefix(ViewName.Name) %> ");
if(strWhere.Trim()!="")
{
strSql.Append(" where "+strWhere);
}
strSql.Append(" order by " + filedOrder);
return DbHelperSQL.Query(strSql.ToString());
}
///
/// 获取记录总数
///
public int GetRecordCount(string strWhere)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("select count(*) FROM <%=ClearPrefix(ViewName.Name) %> ");
if(strWhere.Trim()!="")
{
strSql.Append(" where "+strWhere);
}
object obj = DbHelperSQL.GetSingle(strSql.ToString());
if (obj == null)
{
return 0;
}
else
{
return Convert.ToInt32(obj);
}
}
///
/// 分页获取数据列表
///
public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("SELECT * FROM ( ");
strSql.Append(" SELECT ROW_NUMBER() OVER (");
if (!string.IsNullOrEmpty(orderby.Trim()))
{
strSql.Append("order by T." + orderby );
}
strSql.Append(")AS Row, T.* from <%=ClearPrefix(ViewName.Name) %> T ");
if (!string.IsNullOrEmpty(strWhere.Trim()))
{
strSql.Append(" WHERE " + strWhere);
}
strSql.Append(" ) TT");
strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
return DbHelperSQL.Query(strSql.ToString());
}
#endregion Method
#region MethodEx
#endregion MethodEx
}
}