DAL模版
<#@ template language="c#" HostSpecific="True" #>
<#@ output extension= ".cs" #>
<#
TableHost host = (TableHost)(Host);
string DbParaHead=host.DbParaHead;
string DbParaDbType=host.DbParaDbType;
string preParameter=host.preParameter;
string ModelSpace = host.NameSpace+".Model."+ host.GetModelClass(host.TableName);
ColumnInfo identityKey=host.IdentityKey;
string returnValue = "void";
if (identityKey!=null)
{
returnValue = CodeCommon.DbTypeToCS(identityKey.TypeName);
}
#>
using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using DBUtility;
using Model;
namespace DAL
<# if( host.Folder.Length > 0){ #>
.<#= host.Folder #>
<# } #>
{
<# if( host.TableDescription.Length > 0) {#>
//<#= host.TableDescription #>
<# } #>
public partial class DAL<#= host.GetDALClass(host.TableName) #>
{
public bool Exists(<#= CodeCommon.GetInParameter(host.Keys, false) #>)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("select count(1) from <#= host.TableName #>");
strSql.Append(" where ");
<# for(int i=0;i< host.Keys.Count;i++)
{ ColumnInfo key = host.Keys[i]; #>
<# if (key.IsPrimaryKey || !key.IsIdentity)
{#>
strSql.Append(" <#= key.ColumnName#> = <#=preParameter#><#=key.ColumnName#> <# if (i< host.Keys.Count-1 ) {#>and <#}#> ");
<#}#>
<# }#>
<#= CodeCommon.GetPreParameter(host.Keys, false, host.DbType) #>
return DBExecuteUtil.Exists(strSql.ToString(),parameters);
}
/// <summary>
/// 增加一条数据
/// </summary>
public <#= returnValue #> Add(<#= host.TableName #>Entity model)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("insert into <#= host.TableName #>(");
strSql.Append("<# for(int i=0;i< host.Fieldlist.Count;i++) { ColumnInfo c = host.Fieldlist[i]; if (!c.IsIdentity) {#><#= c.ColumnName#><# if (i< host.Fieldlist.Count-1 ) {#>,<#}#><#}}#>");
strSql.Append(") values (");
strSql.Append("<# for(int i=0;i< host.Fieldlist.Count;i++) { ColumnInfo c = host.Fieldlist[i]; if (!c.IsIdentity) {#><#=preParameter#><#= c.ColumnName#><# if (i< host.Fieldlist.Count-1 ) {#>,<#}#><#}}#>");
strSql.Append(") ");
<#if (identityKey!=null) {#>strSql.Append(";select @@IDENTITY");<#}#>
SqlParameter[] parameters = {
<# for(int i=0;i< host.Fieldlist.Count;i++)
{
ColumnInfo c = host.Fieldlist[i];
if(c.IsIdentity) continue;
#>
new SqlParameter("<#=preParameter#><#=c.ColumnName#>", SqlDbType.<#=CodeCommon.DbTypeLength(host.DbType, c.TypeName, c.Length)#>) <# if (i< host.Fieldlist.Count-1 ) {#>,<#}#>
<# }#>
};
<# foreach (ColumnInfo c in host.Fieldlist) { if(c.IsIdentity) continue;#>
parameters[<#= n #>].Value = <# if ("uniqueidentifier" == c.TypeName.ToLower()){#>Guid.NewGuid();<#} else {#>model.<#=c.ColumnName#>;<#} n=n+1; #>
<# }#>
<#if (identityKey!=null) {#>
object obj = <#= host.DbHelperName#>.GetSingle(strSql.ToString(),parameters);
if (obj == null)
{
return 0;
}
else
{
<# if ( returnValue=="int" )
{#>
return Convert.ToInt32(obj);
<#}#>
<# if ( returnValue=="long" )
{#>
return Convert.ToInt64(obj);
<#}#>
<# if ( returnValue=="decimal" )
{#>
return Convert.ToDecimal(obj);
<#}#>
}
<#} else {#>
DBExecuteUtil.ExecuteSqlWithParams(strSql.ToString(),parameters);
<#}#>
}
/// <summary>
/// 更新一条数据
/// </summary>
public bool Update(<#= host.TableName #>Entity model)
{
StringBuilder strSql=new StringBuilder();
List<SqlParameter> parameters = new List<SqlParameter>();
strSql.Append("update <#= host.TableName #> set ");
<# for(int i=0;i< host.Fieldlist.Count;i++)
{ ColumnInfo c = host.Fieldlist[i]; #>
<# if (!c.IsIdentity) {#>
<# if(CodeCommon.DbTypeToCS(c.TypeName)=="string") {#>
if(!string.IsNullOrEmpty(model.<#=c.ColumnName#>)){
strSql.Append(" <#= c.ColumnName #> = <#=preParameter#><#=c.ColumnName#> , ");
parameters.Add(new SqlParameter("<#=preParameter#><#=c.ColumnName#>",model.<#=c.ColumnName#>));
}<# }#>
<# if(CodeCommon.DbTypeToCS(c.TypeName)=="bool") {#>
strSql.Append(" <#= c.ColumnName #> = <#=preParameter#><#=c.ColumnName#> , ");
parameters.Add(new SqlParameter("<#=preParameter#><#=c.ColumnName#>",model.<#=c.ColumnName#>));
<# }#>
<# if(CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") {#>
if(model.<#=c.ColumnName#>!=null){
strSql.Append(" <#= c.ColumnName #> = <#=preParameter#><#=c.ColumnName#> , ");
parameters.Add(new SqlParameter("<#=preParameter#><#=c.ColumnName#>",model.<#=c.ColumnName#>));
}
<# }#>
<# if(CodeCommon.DbTypeToCS(c.TypeName)=="int"||
CodeCommon.DbTypeToCS(c.TypeName)=="long"||
CodeCommon.DbTypeToCS(c.TypeName)=="float"||
CodeCommon.DbTypeToCS(c.TypeName)=="decimal")
{#>
if(model.<#=c.ColumnName#>!=0){
strSql.Append(" <#= c.ColumnName #> = <#=preParameter#><#=c.ColumnName#> , ");
parameters.Add(new SqlParameter("<#=preParameter#><#=c.ColumnName#>",model.<#=c.ColumnName#>));
}
<#}#>
<#}#>
<# }#>
strSql = strSql.Remove(strSql.Length - 2,2);
strSql.Append(" where <#= CodeCommon.GetWhereParameterExpression(host.Keys, true ,host.DbType) #> ");
<# for(int i=0;i< host.Keys.Count;i++)
{ ColumnInfo key = host.Keys[i]; #>
<# if (key.IsPrimaryKey || !key.IsIdentity)
{#>
parameters.Add(new SqlParameter("<#=preParameter#><#=key.ColumnName#>",model.<#=key.ColumnName#>));
<#}#>
<# }#>
int rows=DBExecuteUtil.ExecuteSqlWithParams(strSql.ToString(),parameters.ToArray());
if (rows > 0)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 删除一条数据
/// </summary>
public bool Delete(<#=CodeCommon.GetInParameter(host.Keys, true)#>)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("delete from <#= host.TableName #> ");
strSql.Append(" where <#= CodeCommon.GetWhereParameterExpression(host.Keys, true, host.DbType)#>");
<#= CodeCommon.GetPreParameter(host.Keys, true, host.DbType) #>
int rows=DBExecuteUtil.ExecuteSqlWithParams(strSql.ToString(),parameters);
if (rows > 0)
{
return true;
}
else
{
return false;
}
}
<#if (identityKey!=null) {#>
/// <summary>
/// 批量删除一批数据
/// </summary>
public bool DeleteList(string <#=identityKey.ColumnName#>list )
{
StringBuilder strSql=new StringBuilder();
strSql.Append("delete from <#= host.TableName #> ");
strSql.Append(" where ID in ("+<#=identityKey.ColumnName#>list + ") ");
int rows=<#= host.DbHelperName#>.ExecuteSql(strSql.ToString());
if (rows > 0)
{
return true;
}
else
{
return false;
}
}
<#}#>
/// <summary>
/// 得到一个对象实体
/// </summary>
public <#= host.TableName #>Entity GetModel(<#= CodeCommon.GetInParameter(host.Keys,true) #>)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("select <# for(int i=0;i< host.Fieldlist.Count;i++) { #><#= host.Fieldlist[i].ColumnName #><# if(i< host.Fieldlist.Count-1 ) {#>,<# } #> <#}#> ");
strSql.Append(" from <#= host.TableName #> ");
strSql.Append(" where <#= CodeCommon.GetWhereParameterExpression(host.Keys, true, host.DbType) #>");
<#=CodeCommon.GetPreParameter(host.Keys, true, host.DbType)#>
<#= host.TableName #>Entity model=new <#= host.TableName #>Entity();
DataTable dt = DBExecuteUtil.querySqlTable(strSql.ToString(),parameters);
if(dt.Rows.Count>0)
{
<# foreach (ColumnInfo c in host.Fieldlist) { #>
<# if(CodeCommon.DbTypeToCS(c.TypeName)=="int"||
CodeCommon.DbTypeToCS(c.TypeName)=="long"||
CodeCommon.DbTypeToCS(c.TypeName)=="float"||
CodeCommon.DbTypeToCS(c.TypeName)=="DateTime"||
CodeCommon.DbTypeToCS(c.TypeName)=="decimal")
{#>
if(dt.Rows[0]["<#=c.ColumnName#>"].ToString()!="")
{
model.<#=c.ColumnName#>=<#=CodeCommon.DbTypeToCS(c.TypeName)#>.Parse(dt.Rows[0]["<#=c.ColumnName#>"].ToString());
}
<# } #>
<# if(CodeCommon.DbTypeToCS(c.TypeName)=="string") {#>
model.<#=c.ColumnName#>= dt.Rows[0]["<#=c.ColumnName#>"].ToString();
<# } #>
<# if(CodeCommon.DbTypeToCS(c.TypeName)=="byte[]") {#>
if(dt.Rows[0]["<#=c.ColumnName#>"].ToString()!="")
{
model.<#=c.ColumnName#>= (byte[])dt.Rows[0]["<#=c.ColumnName#>"];
}
<# } #>
<# if(CodeCommon.DbTypeToCS(c.TypeName)=="Guid") {#>
if(dt.Rows[0]["<#=c.ColumnName#>"].ToString()!="")
{
model.<#=c.ColumnName#>= dt.Rows[0]["<#=c.ColumnName#>"].ToString();
}
<# } #>
<# if(CodeCommon.DbTypeToCS(c.TypeName)=="bool") {#>
if(dt.Rows[0]["<#=c.ColumnName#>"].ToString()!="")
{
if((dt.Rows[0]["<#=c.ColumnName#>"].ToString()=="1")||(dt.Rows[0]["<#=c.ColumnName#>"].ToString().ToLower()=="true"))
{
model.<#=c.ColumnName#>= true;
}
else
{
model.<#=c.ColumnName#>= false;
}
}
<# } #>
<# } #>
return model;
}
else
{
return null;
}
}
/// <summary>
/// 获得数据列表
/// </summary>
public DataTable GetList(string strWhere)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("select * ");
strSql.Append(" FROM <#= host.TableName #> ");
if(strWhere.Trim()!="")
{
strSql.Append(" where "+strWhere);
}
return DBExecuteUtil.querySqlTable(strSql.ToString());
}
/// <summary>
/// 获得前几行数据
/// </summary>
public DataTable GetList(int Top,string strWhere,string filedOrder)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("select ");
if(Top>0)
{
strSql.Append(" top "+Top.ToString());
}
strSql.Append(" * ");
strSql.Append(" FROM <#= host.TableName #> ");
if(strWhere.Trim()!="")
{
strSql.Append(" where "+strWhere);
}
strSql.Append(" order by " + filedOrder);
return DBExecuteUtil.querySqlTable(strSql.ToString());
}
}
}
<#+
int n=0;
#>
Model模版
<#@ template language="c#" HostSpecific="True" #>
<#@ output extension= ".cs" #>
<#
TableHost host = (TableHost)(Host);
host.Fieldlist.Sort(CodeCommon.CompareByintOrder);
#>
using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
namespace Model<# if( host.Folder.Length > 0) {#>.<#= host.Folder #><# } #>
{
<# if( host.TableDescription.Length > 0) {#>
//<#= host.TableDescription #>
<# } #>
public class <#= host.GetModelClass(host.TableName) #>Entity
{
<# foreach (ColumnInfo c in host.Fieldlist)
{ #>/// <summary>
/// <#= string.IsNullOrEmpty(c.Description) ? c.ColumnName : c.Description #>
/// </summary>
public <#= CodeCommon.DbTypeToCS(c.TypeName) #> <#= c.ColumnName #>{get;set;}
<# } #>
}
}
BLL模版
<#@ template language="c#" HostSpecific="True" #>
<#@ output extension= ".cs" #>
<#
TableHost host = (TableHost)(Host);
string ModelSpace = host.NameSpace+".Model."+ host.GetModelClass(host.TableName);
string DALSpace= host.NameSpace+".DAL."+ host.GetDALClass(host.TableName);
ColumnInfo identityKey=host.IdentityKey;
string returnValue = "void";
if (identityKey!=null)
{
returnValue = CodeCommon.DbTypeToCS(identityKey.TypeName);
}
#>
using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using DAL;
using Model;
namespace BLL <# if( host.Folder.Length > 0){ #>.<#= host.Folder #><# } #>
{
<# if( host.TableDescription.Length > 0) {#>
//<#= host.TableDescription #>
<# } #>
public partial class BLL<#= host.GetBLLClass(host.TableName) #>
{
private readonly DAL<#= host.TableName #> dal=new DAL<#= host.TableName #>();
public BLL<#= host.GetBLLClass(host.TableName) #>()
{}
#region Method
/// <summary>
/// 是否存在该记录
/// </summary>
public bool Exists(<#= CodeCommon.GetInParameter(host.Keys, false) #>)
{
return dal.Exists(<#= CodeCommon.GetFieldstrlist(host.Keys, false)#>);
}
/// <summary>
/// 增加一条数据
/// </summary>
public <#= returnValue #> Add(<#= host.TableName #>Entity model)
{
<#if (identityKey!=null) {#>
return dal.Add(model);
<#} else {#>
dal.Add(model);
<#}#>
}
/// <summary>
/// 更新一条数据
/// </summary>
public bool Update(<#= host.TableName #>Entity model)
{
return dal.Update(model);
}
/// <summary>
/// 删除一条数据
/// </summary>
public bool Delete(<#=CodeCommon.GetInParameter(host.Keys, true)#>)
{
return dal.Delete(<#=CodeCommon.GetFieldstrlist(host.Keys, true)#>);
}
<#if (identityKey!=null) {#>
/// <summary>
/// 批量删除一批数据
/// </summary>
public bool DeleteList(string <#=identityKey.ColumnName#>list )
{
return dal.DeleteList(<#=identityKey.ColumnName#>list );
}
<#}#>
/// <summary>
/// 得到一个对象实体
/// </summary>
public <#= host.TableName #>Entity GetModel(<#= CodeCommon.GetInParameter(host.Keys,true) #>)
{
return dal.GetModel(<#=CodeCommon.GetFieldstrlist(host.Keys, true)#>);
}
/// <summary>
/// 得到一个对象实体,从缓存中
/// </summary>
//public <#= host.TableName #>Entity GetModelByCache(<#= CodeCommon.GetInParameter(host.Keys,true) #>)
//{
//
// string CacheKey = "<#= host.TableName #>Model-" + <#=CodeCommon.GetFieldstrlistAdd(host.Keys, true)#>;
// object objModel = Common.DataCache.GetCache(CacheKey);
// if (objModel == null)
// {
// try
// {
// objModel = dal.GetModel(<#=CodeCommon.GetFieldstrlist(host.Keys, true)#>);
// if (objModel != null)
// {
// int ModelCache = Common.ConfigHelper.GetConfigInt("ModelCache");
// Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
// }
// }
// catch{}
// }
// return (<#= host.TableName #>Entity)objModel;
//}
/// <summary>
/// 获得数据列表
/// </summary>
public DataTable GetList(string strWhere)
{
return dal.GetList(strWhere);
}
/// <summary>
/// 获得前几行数据
/// </summary>
public DataTable GetList(int Top,string strWhere,string filedOrder)
{
return dal.GetList(Top,strWhere,filedOrder);
}
/// <summary>
/// 获得数据列表
/// </summary>
public List<<#= host.TableName #>Entity> GetModelList(string strWhere)
{
DataTable dt = dal.GetList(strWhere);
return DataTableToList(dt);
}
/// <summary>
/// 获得数据列表
/// </summary>
public List<<#= host.TableName #>Entity> DataTableToList(DataTable dt)
{
List<<#= host.TableName #>Entity> modelList = new List<<#= host.TableName #>Entity>();
int rowsCount = dt.Rows.Count;
if (rowsCount > 0)
{
<#= host.TableName #>Entity model;
for (int n = 0; n < rowsCount; n++)
{
model = new <#= host.TableName #>Entity();
<# foreach (ColumnInfo c in host.Fieldlist) { #>
<# if(CodeCommon.DbTypeToCS(c.TypeName)=="int"||
CodeCommon.DbTypeToCS(c.TypeName)=="long"||
CodeCommon.DbTypeToCS(c.TypeName)=="float"||
CodeCommon.DbTypeToCS(c.TypeName)=="DateTime"||
CodeCommon.DbTypeToCS(c.TypeName)=="decimal")
{#>
if(dt.Rows[n]["<#=c.ColumnName#>"].ToString()!="")
{
model.<#=c.ColumnName#>=<#=CodeCommon.DbTypeToCS(c.TypeName)#>.Parse(dt.Rows[n]["<#=c.ColumnName#>"].ToString());
}
<# } #>
<# if(CodeCommon.DbTypeToCS(c.TypeName)=="string") {#>
model.<#=c.ColumnName#>= dt.Rows[n]["<#=c.ColumnName#>"].ToString();
<# } #>
<# if(CodeCommon.DbTypeToCS(c.TypeName)=="byte[]") {#>
if(dt.Rows[n]["<#=c.ColumnName#>"].ToString()!="")
{
model.<#=c.ColumnName#>= (byte[])dt.Rows[n]["<#=c.ColumnName#>"];
}
<# } #>
<# if(CodeCommon.DbTypeToCS(c.TypeName)=="Guid") {#>
if(dt.Rows[n]["<#=c.ColumnName#>"].ToString()!="")
{
model.<#=c.ColumnName#>= dt.Rows[n]["<#=c.ColumnName#>"].ToString();
}
<# } #>
<# if(CodeCommon.DbTypeToCS(c.TypeName)=="bool") {#>
if(dt.Rows[n]["<#=c.ColumnName#>"].ToString()!="")
{
if((dt.Rows[n]["<#=c.ColumnName#>"].ToString()=="1")||(dt.Rows[n]["<#=c.ColumnName#>"].ToString().ToLower()=="true"))
{
model.<#=c.ColumnName#>= true;
}
else
{
model.<#=c.ColumnName#>= false;
}
}
<# } #>
<# } #>
modelList.Add(model);
}
}
return modelList;
}
/// <summary>
/// 获得数据列表
/// </summary>
public DataTable GetAllList()
{
return GetList("");
}
#endregion
}
}
WEB模版
<Rows>
<# int i=0;#>
<# foreach (ColumnInfo c in host.Fieldlist)
{ #>
<# if(i%2==0){#>
<ext:FormRow ID="frow_<#= c.ColumnName #>" runat="server">
<# } #><Items>
<# if(CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") { #>
<ext:DatePicker runat="server" Required="true" Label="<#= c.ColumnName #>" EmptyText="限制范围的日期" ID="dp<#= c.ColumnName #>" ShowRedStar="True" Width="200px"></ext:DatePicker>
<#} else #>
<# if(CodeCommon.DbTypeToCS(c.TypeName)=="bool") { #>
<ext:CheckBoxList runat="server" ID="chkl<#= c.ColumnName #>" Label="chk<#= c.ColumnName #>" ShowLabel="true" Width="200px"><ext:CheckItem Text="<#= c.ColumnName #>" /></ext:CheckBoxList>
<#} else #>
<# if(CodeCommon.DbTypeToCS(c.TypeName)=="string") { #>
<ext:TextBox ID="txt<#= c.ColumnName #>" runat="server" Required="true" Label="<#= c.Description == "" ? c.ColumnName : c.Description #>" ShowLabel="true" Width="200px"></ext:TextBox>
<#} else #>
<# if(CodeCommon.DbTypeToCS(c.TypeName)=="int") { #>
<ext:TextBox ID="txt<#= c.ColumnName #>" runat="server" Required="true" RegexPattern="NUMBER" Label="<#= c.Description == "" ? c.ColumnName : c.Description #>" ShowLabel="true" Width="200px"></ext:TextBox>
<#}#></Items>
<# if(i%2==1||i==host.Fieldlist.Count-1){#>
</ext:FormRow>
<# } #>
<# i++; #>
<# } #>
<ext:FormRow runat="server"><Items><ext:Button ConfirmText="确认保存?" Id="btnSubmit" Text="" ValidateForms="Form1" runat="server"></ext:Button></Items></ext:FormRow>
</Rows>
<script src="/js/calendar1.js" type="text/javascript"></script>
作者:石世特
出处:http://www.cnblogs.com/TivonStone/
希望本文对你有所帮助,想转随便转,心情好的话给我的文章留个链接.o(. .)o
浙公网安备 33010602011771号