在前后台的数据交互中常常会涉及到这样的情形,前台传递一个条件,后台往往一次都是只能返回一个统计结果,于是乎当需要多个统计结果的时候,很多都采用多次调用前后台交互方法来获取,这样就往往会做了多次的数据库交互,浪费了不少资源,如果能一次性返回的话,只执行一次肯定效率也会有不少的提高,当然最简单的一种你可以采用及其特殊的连接符把所有的结果连接为一串普通字符串,但那是不推荐的,还有一种就是封装成为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字符串
var value = {
"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 countryObj in value)
{
document.write(countryObj + ":
")
//没用的for(var cityObj in value.countryObj)
for(var cityObj in value[countryObj])
{
document.write(' ' + cityObj + "
");
for(var itemObj in value[countryObj][cityObj])
{
document.write(" "+ itemObj + value[countryObj][cityObj][itemObj] +"
")
}
}
}
1、自定义一个数据类
public class DataImfo
{
private string actualInvest;//年度实际投资
private string yearInvest;//年度计划投资
public string ActualInvest
{
set
{
actualInvest = value;
}
get
{
return actualInvest;
}
}
public string YearInvest
{
set
{
yearInvest = value;
}
get
{
return yearInvest;
}
}
}
DataImfo dt = new DataImfo();
dt.ActualInvest = "1";
dt.YearInvest = "222";
string imfo = JsonConvert.SerializeObject(dt).ToString();
///
/// 获取月度资金计划的表单中的 截至申报日实际完成投资、
///
/// 表单上所选的日期
/// 合同Id
///
[LogicMethod]
public string GetPayImfo(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;
}
====前台解析:
var imfo=Ajax.WebRequest("Biz/PMIS_SX/PMIS_SX_ZJGL_Service/GetPayImfo",{month:month, contractId: contractId });
var data=eval('('+imfo+')');
if(data!="" &&data!=undefined)
{
var actualInvest=data.ActualInvest;
var yearInvest=data.YearInvest;
var sumAmount=data.SumContractAmount;//合同金额合计
var exeAmount=data.ExedMoney;//已执行合同
}
以目前笔者的水平,我也只能想到以上这两种方式了,也基本满足的笔者在开发PDM和PMIS系统中的业务需求,如有更好的方案还望各位不吝赐教!