json调用类
IHttpHandler { public void ProcessRequest(HttpContext context) { if (context.Request["operate_type"] == "operate_type1") { GetOperate_type1(context); } else if (context.Request["operate_type"] == "operate_type2") { //GetOperate_type2(context); } } private void GetOperate_type1(HttpContext context) { context.Response.Clear(); context.Response.Buffer = true; context.Response.ContentType = "application/json"; context.Response.ContentEncoding.GetBytes("utf-8"); var sql = ""; System.Data.DataTable dt_ = DBUtility.DBExecuteUtil.querySqlTable(sql); string json = "{"rows":["; for (int i = 0; i < dt_.Rows.Count; i++) { json += "{"col1":"" + dt_.Rows[i]["col1"] + "","col2":"" + dt_.Rows[i]["col2"] + ""},"; } json = json.TrimEnd(','); json += "]}"; context.Response.Write(json); context.Response.Flush(); context.Response.End(); } public bool IsReusable { get { return false; } } }
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 自动代码 /// <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)#>); } public bool Delete(string strWhere) { return dal.Delete(strWhere); } <#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 DataTable GetAllList4Table() { return GetList4Table(""); } /// <summary> /// 获得数据列表 /// </summary> public String[][] GetAllList4Array() { return GetList4Array(""); } /// <summary> /// 获得数据列表 /// </summary> public DataTable GetList4Table(string strWhere) { return dal.GetList4Table(strWhere); } /// <summary> /// 获得数据列表 /// </summary> public String[][] GetList4Array(string strWhere) { return dal.GetList4Array(strWhere); } /// <summary> /// 获得前几行数据 /// </summary> public DataTable GetList4Table(int Top,string strWhere,string filedOrder) { return dal.GetList4Table(Top,strWhere,filedOrder); } /// <summary> /// 获得前几行数据 /// </summary> public String[][] GetList4Array(int Top,string strWhere,string filedOrder) { return dal.GetList4Array(Top,strWhere,filedOrder); } /// <summary> /// 获得数据列表 /// </summary> public List<<#= host.TableName #>Entity> GetModelList(string strWhere) { DataTable dt = dal.GetList4Table(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; int result; 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)=="int") {#> model.<#=c.ColumnName#> = int.TryParse(dt.Rows[n]["<#=c.ColumnName#>"].ToString(), out result) ? result : 0; <# } #> <# 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#>= Guid.Parse(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 int GetCount(string strWhere) { return dal.GetCount(strWhere); } #endregion #region 人工代码 /// <summary> /// 获得分页数据 /// </summary> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <param name="model"></param> /// <returns></returns> public DataTable GetPage(int pageIndex, int pageSize, <#=host.TableName#>Entity model,out int count) { return dal.GetPage(pageIndex, pageSize, model,out count); } #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 = 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 System.Collections; 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) #> { #region 自动代码 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 SCOPE_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) {#> DataTable dt = DBExecuteUtil.querySqlTable(strSql.ToString(),parameters); if (dt.Rows.Count == 0) { <# if ( returnValue=="int" ) {#> return 0; <#} else#> <# if ( returnValue=="long" ) {#> return 0; <#} else#> <# if ( returnValue=="decimal" ) {#> return 0; <#}#> } else { <# if ( returnValue=="int" ) {#> return Convert.ToInt32(dt.Rows[0][0].ToString()); <#}#> <# if ( returnValue=="long" ) {#> return Convert.ToInt64(dt.Rows[0][0].ToString()); <#}#> <# if ( returnValue=="decimal" ) {#> return Convert.ToDecimal(dt.Rows[0][0].ToString()); <#}#> } <#} 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 && !c.IsPrimaryKey) {#> <# 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(true){ 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; } } public bool Delete(string strWhere) { StringBuilder strSql=new StringBuilder(); strSql.Append("delete from <#= host.TableName #> "); if(!string.IsNullOrEmpty(strWhere)) { strSql.Append(" where "+strWhere); } else{ return false; } int rows = DBExecuteUtil.ExecuteSql(strSql.ToString()); 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=DBExecuteUtil.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#>= Guid.Parse(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> /// <param name="strWhere"></param> /// <returns>datatable</returns> public DataTable GetList4Table(string strWhere) { StringBuilder strSql=new StringBuilder(); strSql.Append("select * "); strSql.Append(" FROM <#= host.TableName #> "); if(!string.IsNullOrEmpty(strWhere)) { strSql.Append(" where "+strWhere); } return DBExecuteUtil.querySqlTable(strSql.ToString()); } /// <summary> /// 获得数据列表 /// </summary> /// <param name="strWhere"></param> /// <returns>array</returns> public String[][] GetList4Array(string strWhere) { StringBuilder strSql=new StringBuilder(); strSql.Append("select * "); strSql.Append(" FROM <#= host.TableName #> "); if(!string.IsNullOrEmpty(strWhere)) { strSql.Append(" where "+strWhere); } return DBExecuteUtil.querySqlArray(strSql.ToString()); } /// <summary> /// 获得数据列表 /// </summary> /// <param name="strWhere"></param> /// <returns>datatable</returns> public DataTable GetList4Table(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 (!string.IsNullOrEmpty(strWhere)) { strSql.Append(" where " + strWhere); } if (!string.IsNullOrEmpty(strWhere)) { strSql.Append(" order by " + filedOrder); } return DBExecuteUtil.querySqlTable(strSql.ToString()); } /// <summary> /// 获得数据列表 /// </summary> /// <param name="strWhere"></param> /// <returns>array</returns> public String[][] GetList4Array(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 (!string.IsNullOrEmpty(strWhere)) { strSql.Append(" where " + strWhere); } if (!string.IsNullOrEmpty(strWhere)) { strSql.Append(" order by " + filedOrder); } return DBExecuteUtil.querySqlArray(strSql.ToString()); } /// <summary> /// 获取数据总数 /// </summary> /// <param name="strWhere"></param> /// <returns></returns> public int GetCount(string strWhere) { StringBuilder strSql = new StringBuilder(); strSql.Append("select count(*) "); strSql.Append(" FROM <#= host.TableName #> "); if (!string.IsNullOrEmpty(strWhere)) { strSql.Append(" where " + strWhere); } string str_count = DBExecuteUtil.querySqlTable(strSql.ToString()).Rows[0][0].ToString(); int int_count = 0; bool b_count = int.TryParse(str_count,out int_count); return int_count; } #endregion #region 人工代码 /// <summary> /// 获取分页数据 /// </summary> /// <returns></returns> public DataTable GetPage(int pageIndex, int pageSize,<#= host.TableName #>Entity model,out int count) { count = 0; SqlParameter[] items=new SqlParameter[]{ new SqlParameter("pageIndex",pageIndex), new SqlParameter("pageSize",pageSize), new SqlParameter("count",count) }; items[2].Direction = ParameterDirection.Output; var dt= DBStoreProUtil.queryProTable4Params("proc_<#= host.TableName #>_Pagedata", items); count=int.Parse(items[2].Value.ToString()); return dt; } #endregion } } <#+ 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) { #> public <#= CodeCommon.DbTypeToCS(c.TypeName) #> <#= c.ColumnName #>{get;set;} <# } #> } }