自动代码模板文件
bll:
<#@ template language="c#" HostSpecific="True" #> <#@ output extension= ".cs" #> <# TableHost host = (TableHost)(Host); string ModelSpace = "Model."+ host.GetModelClass(host.TableName); string DALSpace="DAL."+ host.GetDALClass(host.TableName); ColumnInfo identityKey=host.IdentityKey; string returnValue = "void"; if (identityKey!=null) { returnValue = CodeCommon.DbTypeToCS(identityKey.TypeName); } #> using System; using System.Data; using System.Collections.Generic; using Model; using DAL; namespace BLL <# if( host.Folder.Length > 0){ #>.<#= host.Folder #><# } #> { <# if( host.TableDescription.Length > 0) {#> //<#= host.TableDescription #> <# } #> public partial class <#= host.GetBLLClass(host.TableName) #> { private readonly <#= DALSpace #> dal=new <#= DALSpace #>(); public <#= 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(<#= ModelSpace #> model) { <#if (identityKey!=null) {#>return dal.Add(model);<#} else {#>dal.Add(model);<#}#> } /// <summary> /// 更新一条数据 /// </summary> public bool Update(<#= ModelSpace #> 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 <#= ModelSpace #> GetModel(<#= CodeCommon.GetInParameter(host.Keys,true) #>) { return dal.GetModel(<#=CodeCommon.GetFieldstrlist(host.Keys, true)#>); } /// <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<<#= ModelSpace #>> GetModelList(string strWhere) { DataTable dt = dal.GetList(strWhere); return DataTableToList(dt); } /// <summary> /// 获得数据列表 /// </summary> public List<<#= ModelSpace #>> DataTableToList(DataTable dt) { List<<#= ModelSpace #>> modelList = new List<<#= ModelSpace #>>(); int rowsCount = dt.Rows.Count; if (rowsCount > 0) { <#= ModelSpace #> model; for (int n = 0; n < rowsCount; n++) { model = new <#= ModelSpace #>(); <# 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(""); } /// <summary> /// GetPage("gs_id>3", "gs_id", "order by gs_id", 2, 3, out recordCount, out pageCount); /// </summary> /// <param name="Where">"" or "gs_id>3"</param> /// <param name="IndexField">"gs_id"</param> /// <param name="OrderFields">"order by gs_id" or "order by gs_id desc"</param> /// <param name="PageIndex">2</param> /// <param name="PageSize">3</param> /// <param name="RecordCount">out recordCount</param> /// <param name="PageCount">out pageCount</param> /// <returns></returns> public DataTable GetPage(string Where, string IndexField, string OrderFields, int PageIndex, int PageSize, out int RecordCount, out int PageCount) { return dal.GetPage(Where, IndexField, OrderFields, PageIndex, PageSize, out RecordCount, out PageCount); } #endregion } }
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 = "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 DBUtilityLB; namespace DAL <# if( host.Folder.Length > 0){ #> .<#= host.Folder #> <# } #> { <# if( host.TableDescription.Length > 0) {#> //<#= host.TableDescription #> <# } #> public partial class <#= 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) #> object ret = <#= host.DbHelperName#>.ExecuteScalar(CommandType.Text, strSql.ToString(), parameters); return (int)ret == 0 ? false : true; } /// <summary> /// 增加一条数据 /// </summary> public <#= returnValue #> Add(<#= ModelSpace #> 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#>.ExecuteScalar(CommandType.Text, 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 {#> <#= host.DbHelperName#>.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters); <#}#> } /// <summary> /// 更新一条数据 /// </summary> public bool Update(<#= ModelSpace #> model) { StringBuilder strSql=new StringBuilder(); strSql.Append("update <#= host.TableName #> set "); <# for(int i=0;i< host.Fieldlist.Count;i++) { ColumnInfo c = host.Fieldlist[i]; #> <# if (!c.IsIdentity) {#> strSql.Append(" <#= c.ColumnName #> = <#=preParameter#><#=c.ColumnName#> <# if (i< host.Fieldlist.Count-1 ) {#>,<#}#> ");<# }#> <# }#> strSql.Append(" where <#= CodeCommon.GetWhereParameterExpression(host.Keys, true ,host.DbType) #> "); SqlParameter[] parameters = { <# for(int i=0;i< host.Fieldlist.Count;i++) { ColumnInfo c = host.Fieldlist[i]; #> new SqlParameter("<#=preParameter#><#=c.ColumnName#>", SqlDbType.<#=CodeCommon.DbTypeLength(host.DbType, c.TypeName, c.Length)#>) <# if (i< host.Fieldlist.Count-1 ) {#>,<#}#> <# }#> }; <# n=0; #> <# foreach (ColumnInfo c in host.Fieldlist) { #> parameters[<#= n #>].Value = model.<#=c.ColumnName#>;<# n=n+1; #> <# }#> int rows=<#= host.DbHelperName#>.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters); return rows > 0 ? true : 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=<#= host.DbHelperName#>.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters); return rows > 0 ? true : 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#>.ExecuteNonQuery(CommandType.Text, strSql.ToString(),null); return rows > 0 ? true : false; } <#}#> /// <summary> /// 得到一个对象实体 /// </summary> public <#= ModelSpace #> 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)#> <#=ModelSpace#> model=null; DataTable dt=<#= host.DbHelperName#>.ExecuteTable(CommandType.Text, strSql.ToString(), parameters); if(dt.Rows.Count>0) { model=new <#=ModelSpace#>(); <# 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; } /// <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 <#= host.DbHelperName#>.ExecuteTable(CommandType.Text, strSql.ToString(), null); } /// <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 <#= host.DbHelperName#>.ExecuteTable(CommandType.Text, strSql.ToString(), null); } /// <summary> /// GetPage("gs_id>3", "gs_id", "order by gs_id", 2, 3, out recordCount, out pageCount); /// </summary> /// <param name="Where">"" or "gs_id>3"</param> /// <param name="IndexField">"gs_id"</param> /// <param name="OrderFields">"order by gs_id" or "order by gs_id desc"</param> /// <param name="PageIndex">2</param> /// <param name="PageSize">3</param> /// <param name="RecordCount">out recordCount</param> /// <param name="PageCount">out pageCount</param> /// <returns></returns> public DataTable GetPage(string Where, string IndexField, string OrderFields, int PageIndex, int PageSize, out int RecordCount, out int PageCount) { string SqlTablesAndWhere = string.IsNullOrEmpty(Where.Trim()) ? "<#= host.TableName #>" : "<#= host.TableName #> where " + Where; return DbHelperSQL.ExecutePage("*", SqlTablesAndWhere, IndexField, OrderFields, PageIndex, PageSize, out RecordCount, out PageCount, null); } } } <#+ 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 { //goodsMaster public class <#= host.GetModelClass(host.TableName) #> { <# foreach (ColumnInfo c in host.Fieldlist){#> private <#= CodeCommon.DbTypeToCS(c.TypeName) #> _<#= c.ColumnName.ToString().ToLower() #>; public <#= CodeCommon.DbTypeToCS(c.TypeName) #> <#= c.ColumnName #> { get { return _<#= c.ColumnName.ToString().ToLower()#>; } set { _<#= c.ColumnName.ToString().ToLower()#> = value; } } <#}#> } }
oc model
<#@ template language="c#" HostSpecific="True" #> <#@ output extension= ".h" #> <# TableHost host = (TableHost)(Host); host.Fieldlist.Sort(CodeCommon.CompareByintOrder); #> #import <Foundation/Foundation.h> @interface LSModel_<#= host.GetModelClass(host.TableName) #> : NSObject <# foreach (ColumnInfo c in host.Fieldlist){#> <#if(CodeCommon.DbTypeToCS(c.TypeName)=="int"){#> @property (nonatomic)NSInteger <#= c.ColumnName.ToString()#>; <#} else if(CodeCommon.DbTypeToCS(c.TypeName)=="string"){#> @property (strong,nonatomic) NSString *<#= c.ColumnName.ToString()#>; <#} else if(CodeCommon.DbTypeToCS(c.TypeName)=="decimal"){#> @property (nonatomic) float <#= c.ColumnName.ToString()#>; <#} else if(CodeCommon.DbTypeToCS(c.TypeName)=="DateTime"){#> @property (strong,nonatomic) NSDate * <#= c.ColumnName.ToString()#>; <#} else{#> @property (strong,nonatomic) NSString *<#= c.ColumnName.ToString()#>; <#}#> <#}#> @end
ocmodel -impl
<#@ template language="c#" HostSpecific="True" #> <#@ output extension= ".m" #> <# TableHost host = (TableHost)(Host); host.Fieldlist.Sort(CodeCommon.CompareByintOrder); #> #import "<#= host.GetModelClass(host.TableName) #>.h" @implementation LSModel_<#= host.GetModelClass(host.TableName) #> @end
view bll
<#@ template language="c#" HostSpecific="True" #> <#@ output extension= ".cs" #> <# TableHost host = (TableHost)(Host); string ModelSpace = "Model."+ host.GetModelClass(host.TableName); string DALSpace="DAL."+ host.GetDALClass(host.TableName); ColumnInfo identityKey=host.IdentityKey; string returnValue = "void"; if (identityKey!=null) { returnValue = CodeCommon.DbTypeToCS(identityKey.TypeName); } #> using System; using System.Data; using System.Collections.Generic; using Model; using DAL; namespace BLL <# if( host.Folder.Length > 0){ #>.<#= host.Folder #><# } #> { <# if( host.TableDescription.Length > 0) {#> //<#= host.TableDescription #> <# } #> public partial class <#= host.GetBLLClass(host.TableName) #> { private readonly <#= DALSpace #> dal=new <#= DALSpace #>(); public <#= host.GetBLLClass(host.TableName) #>() {} #region Method /// <summary> /// 是否存在该记录 /// </summary> public bool Exists(<#= CodeCommon.GetInParameter(host.Keys, true) #>) { return dal.Exists(<#= CodeCommon.GetFieldstrlist(host.Keys, true)#>); } /// <summary> /// 得到一个对象实体 /// </summary> public <#= ModelSpace #> GetModel(<#= CodeCommon.GetInParameter(host.Keys,true) #>) { return dal.GetModel(<#=CodeCommon.GetFieldstrlist(host.Keys, true)#>); } /// <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<<#= ModelSpace #>> GetModelList(string strWhere) { DataTable dt = dal.GetList(strWhere); return DataTableToList(dt); } /// <summary> /// 获得数据列表 /// </summary> public List<<#= ModelSpace #>> DataTableToList(DataTable dt) { List<<#= ModelSpace #>> modelList = new List<<#= ModelSpace #>>(); int rowsCount = dt.Rows.Count; if (rowsCount > 0) { <#= ModelSpace #> model; for (int n = 0; n < rowsCount; n++) { model = new <#= ModelSpace #>(); <# 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(""); } /// <summary> /// GetPage("gs_id>3", "gs_id", "order by gs_id", 2, 3, out recordCount, out pageCount); /// </summary> /// <param name="Where">"" or "gs_id>3"</param> /// <param name="IndexField">"gs_id"</param> /// <param name="OrderFields">"order by gs_id" or "order by gs_id desc"</param> /// <param name="PageIndex">2</param> /// <param name="PageSize">3</param> /// <param name="RecordCount">out recordCount</param> /// <param name="PageCount">out pageCount</param> /// <returns></returns> public DataTable GetPage(string Where, string IndexField, string OrderFields, int PageIndex, int PageSize, out int RecordCount, out int PageCount) { return dal.GetPage(Where, IndexField, OrderFields, PageIndex, PageSize, out RecordCount, out PageCount); } #endregion } }
view 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 = "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 DBUtilityLB; namespace DAL <# if( host.Folder.Length > 0){ #> .<#= host.Folder #> <# } #> { <# if( host.TableDescription.Length > 0) {#> //<#= host.TableDescription #> <# } #> public partial class <#= host.GetDALClass(host.TableName) #> { public bool Exists(<#= CodeCommon.GetInParameter(host.Keys, true) #>) { 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, true, host.DbType) #> object ret = <#= host.DbHelperName#>.ExecuteScalar(CommandType.Text, strSql.ToString(), parameters); return (int)ret == 0 ? false : true; } /// <summary> /// 得到一个对象实体 /// </summary> public <#= ModelSpace #> 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)#> <#=ModelSpace#> model=null; DataTable dt=<#= host.DbHelperName#>.ExecuteTable(CommandType.Text, strSql.ToString(), parameters); if(dt.Rows.Count>0) { model=new <#=ModelSpace#>(); <# 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; } /// <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 <#= host.DbHelperName#>.ExecuteTable(CommandType.Text, strSql.ToString(), null); } /// <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 <#= host.DbHelperName#>.ExecuteTable(CommandType.Text, strSql.ToString(), null); } /// <summary> /// GetPage("gs_id>3", "gs_id", "order by gs_id", 2, 3, out recordCount, out pageCount); /// </summary> /// <param name="Where">"" or "gs_id>3"</param> /// <param name="IndexField">"gs_id"</param> /// <param name="OrderFields">"order by gs_id" or "order by gs_id desc"</param> /// <param name="PageIndex">2</param> /// <param name="PageSize">3</param> /// <param name="RecordCount">out recordCount</param> /// <param name="PageCount">out pageCount</param> /// <returns></returns> public DataTable GetPage(string Where, string IndexField, string OrderFields, int PageIndex, int PageSize, out int RecordCount, out int PageCount) { string SqlTablesAndWhere = string.IsNullOrEmpty(Where.Trim()) ? "<#= host.TableName #>" : "<#= host.TableName #> where " + Where; return DbHelperSQL.ExecutePage("*", SqlTablesAndWhere, IndexField, OrderFields, PageIndex, PageSize, out RecordCount, out PageCount, null); } } } <#+ int n=0; #>
java model
:
<#@ template language="c#" HostSpecific="True" #> <#@ output extension= ".h" #> <# TableHost host = (TableHost)(Host); host.Fieldlist.Sort(CodeCommon.CompareByintOrder); #> package app.model; import java.util.Date; public class <#= host.GetModelClass(host.TableName) #> { <# foreach (ColumnInfo c in host.Fieldlist){#> <#if(CodeCommon.DbTypeToCS(c.TypeName)=="int"){#> public int <#= c.ColumnName.ToString()#>; <#} else if(CodeCommon.DbTypeToCS(c.TypeName)=="string"){#> public String <#= c.ColumnName.ToString()#>; <#} else if(CodeCommon.DbTypeToCS(c.TypeName)=="decimal"){#> public float <#= c.ColumnName.ToString()#>; <#} else if(CodeCommon.DbTypeToCS(c.TypeName)=="DateTime"){#> public Date <#= c.ColumnName.ToString()#>; <#} else{#> public String <#= c.ColumnName.ToString()#>; <#}#> <#}#> }