问题引出
在前后台的数据交互中常常会涉及到这样的情形,前台传递一个条件,后台往往一次都是只能返回一个统计结果,于是乎当需要多个统计结果的时候,很多都采用多次调用前后台交互方法来获取,这样就往往会做了多次的数据库交互,浪费了不少资源,如果能一次性返回的话,只执行一次肯定效率也会有不少的提高,当然最简单的一种你可以采用及其特殊的连接符把所有的结果连接为一串普通字符串,但那是不推荐的,还有一种就是封装成为JSON格式的字符串,再返回字符串应该比较高效吧!
相关知识介绍
JSON
[引自度娘]JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集。 JSON采用完全独立于语言的文本格式。这些特性使JSON成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成(网络传输速度)。
//json字符串
json={
info:"Success",
count:"2",data:[
{SYSUSERID:"H-11584",FULLNAME:"贾伟清"},
{SYSUSERID:"H-11765",FULLNAME:"朱庆红"}]
};//json字符串varvalue = {"china":{"hangzhou":{"item":"1"},"shanghai":{"item":"2"},"chengdu":{"item":"3"}
},"America":{"aa":{"item":"1"},"bb":{"item":"2"}
},"Spain":{"dd":{"item":"1"},"ee":{"item":"2"},"ff":{"item":"3"}
}
};//访问json字符串for(var countryObjinvalue)
{
document.write(countryObj +":<br />")//没用的for(var cityObj in value.countryObj)for(var cityObjinvalue[countryObj])
{
document.write(' ' + cityObj +"<br />");for(var itemObjinvalue[countryObj][cityObj])
{
document.write(" "+ itemObj +value[countryObj][cityObj][itemObj] +"<br />")
}
}
}
解决方案
一、自定义一个数据类,然后再通过JsonConvert.SerializeObject方法转化(也叫序列化?)
根据业务所需的数据封装自己的数据类(本例是需要返回年度计划投资、年度实际投资两个统计值,于是乎定义了两个属性用于保存)
1、自定义一个数据类publicclass DataImfo
{privatestring actualInvest;//年度实际投资privatestring yearInvest;//年度计划投资publicstring ActualInvest
{set
{
actualInvest =value;
}get
{return actualInvest;
}
}publicstring YearInvest
{set
{
yearInvest =value;
}get
{return yearInvest;
}
}
}
使用自定义的数据类封装查询结果并返回JSON格式的字符串
DataImfo dt = new DataImfo();
dt.ActualInvest ="1";
dt.YearInvest ="222";
string imfo = JsonConvert.SerializeObject(dt).ToString();
二、利用泛型类,再通过JsonConvert.SerializeObject转化(推荐)
///<summary>/// 获取月度资金计划的表单中的 截至申报日实际完成投资、///</summary>///<param name="month">表单上所选的日期</param>///<param name="contractId">合同Id</param>///<returns></returns>
[LogicMethod]publicstringGetPayImfo(string month,string contractId,string projId)
{string actualInvest =string.Empty;string sumAmount =string.Empty;string yearInvest =string.Empty;string exeAmount =string.Empty;if (String.IsNullOrEmpty(month) ==false && String.IsNullOrEmpty(projId) ==false)
{//month=month.Replace('-',' ');//获取截至申报日实际完成投资string getactualInvest = String.Format(@"Select SUM(PayMoney) ActualInvest
From PS_PaymentOrder a
join RK_ProjectList b on a.CodeNumber=b.ProjectWBSCode
Where IsNull(a.IsDeleted,'0')='0' and CONVERT(varchar(6), PayTime, 112)<='{0}' and b.RK_ProjectListId='{1}'", month, projId);
actualInvest = SysDba.Adapter.ExecuteScalarForString(getactualInvest);//截至申报日本年度完成投资string getYearInvest = String.Format(@"Select SUM(PayMoney) YearInvest
From PS_PaymentOrder a
join RK_ProjectList b on a.CodeNumber=b.ProjectWBSCode
Where IsNull(a.IsDeleted,'0')='0' and CONVERT(varchar(6), PayTime, 112)<='{0}' and b.RK_ProjectListId='{1}'
and CONVERT(varchar(4), PayTime, 112)=CONVERT(varchar(4), '{0}', 112)", month, projId);
yearInvest = SysDba.Adapter.ExecuteScalarForString(getYearInvest);//指定项目下的所有合同金额合计string getSumAmount = String.Format(@"Select sum(c.ContractAmount)
From CG_Contract c
Where IsNull(c.IsDeleted,'0')='0'and c.RK_ProjectListId='{0}'", projId);
sumAmount = SysDba.Adapter.ExecuteScalarForString(getSumAmount);
}if (String.IsNullOrEmpty(contractId) ==false)
{//合同已执行金额string getExeAmount = String.Format(@"Select SUM(PayMoney) PayedAmount
From PS_PaymentOrder a
Left join CG_Contract d on d.Code=a.ContractCode
Where IsNull(a.IsDeleted,'0')='0' and CONVERT(varchar(6), PayTime, 112)<='{0}' and d.CG_ContractId='{1}'", month, contractId);
exeAmount = SysDba.Adapter.ExecuteScalarForString(getExeAmount);
}
Dictionary<string,string> lt =new Dictionary<string,string>();
lt.Add("ActualInvest", actualInvest);
lt.Add("YearInvest", yearInvest);
lt.Add("ExedMoney", exeAmount);
lt.Add("SumContractAmount", sumAmount);string imfo = JsonConvert.SerializeObject(lt).ToString();return imfo;
}
前台解析JSON
====前台解析:var imfo=Ajax.WebRequest("Biz/PMIS_SX/PMIS_SX_ZJGL_Service/GetPayImfo",{month:month, contractId: contractId });vardata=eval('('+imfo+')');if(data!=""&&data!=undefined)
{var actualInvest=data.ActualInvest;var yearInvest=data.YearInvest;var sumAmount=data.SumContractAmount;//合同金额合计var exeAmount=data.ExedMoney;//已执行合同
}
篇后语
以目前笔者的水平,我也只能想到以上这两种方式了,也基本满足的笔者在开发PDM和PMIS系统中的业务需求,如有更好的方案还望各位不吝赐教!