1.在entity文件定义变量
//公司信息
public class futurescompanyinformation
{
public string brokerid;
public string brokername;
public List<Informationtradingaccount> informationtradingaccounts = new List<Informationtradingaccount>(); //将账户信息以数组方式引入
}
//保证金信息
public class fcomcontractsmarginsratios
{
public string brokerid;
public string contractcode;
public string depositratio;
}
//账户信息
public class Informationtradingaccount
{
public string brokername;
public string brokerid;
public string investorid;
public string password;
public string tablefrontaddr;
public string mdfrontaddr;
public string appid;
public string authcode;
public string createid;
public string createtime;
public string Modifiesid;
public string Modifiestime;
public string brokeridm;
}
2.在IYHLH文件
[OperationContract]
//定义 DataTable 函数
DataTable GetCompanyInformationGain();
3.在YHLH文件
public DataTable GetCompanyInformationGain()
{
DataSet ds = new DataSet();
DataHelper dh = new DataHelper();
DataTable dt = new DataTable();
dt.TableName = "DataTable";
List<string> allTrader = new List<string>(); //定义一个数组列表
string sqlstring = @"SELECT [intorid]
,[accountnumber]
,[psword]
,[brokerid]
,[investorid]
,[password]
,[tablefrontaddr]
,[mdfrontaddr]
,[appid]
,[authcode]
,[createid]
,[createtime]
,[Modifiesid]
,[Modifiestime]
,[brokername]
,[brokeridm]
FROM [FierceAnts].[dbo].[View_CompanyInformationGain]"; //sql语句
ds = dh.GetDataSet(sqlstring); //将sql语句装入ds数据表
if (ds.Tables != null && ds.Tables[0].Rows.Count >= 1) //判断表不为空并且条数大于1
{
return ds.Tables[0]; //返回第一行
}
else
{
return null; //否则为null
}
}
4.在servermain文件
[WebMethod]
public string GetCompanyInformationGain()
{
DataTable dt = new DataTable();
List<futurescompanyinformation> futurescompanyinformations = new List<futurescompanyinformation>(); //获取公司信息表
List<Informationtradingaccount> informationtradingaccounts = new List<Informationtradingaccount>(); //获取账户信息表
dt = service.GetCompanyInformationGain(); //调用服务中得方法,获取数据
if (dt != null && dt.Rows.Count > 0) //判断获取得表不等于空并且行数大于0
{
for(int i = 0; i < dt.Rows.Count; i++) //挨个获取每一行得数据
{
Informationtradingaccount infor =new Informationtradingaccount(); //定义函数存放数据
infor.brokername = dt.Rows[i]["brokername"].ToString(); //定义的变量为string,所以需要ToString()转成字符型
infor.brokerid = dt.Rows[i]["brokerid"].ToString();
infor.investorid = dt.Rows[i]["investorid"].ToString();
infor.password = dt.Rows[i]["password"].ToString();
infor.tablefrontaddr = dt.Rows[i]["tablefrontaddr"].ToString();
infor.mdfrontaddr = dt.Rows[i]["mdfrontaddr"].ToString();
infor.appid = dt.Rows[i]["appid"].ToString();
infor.authcode = dt.Rows[i]["authcode"].ToString();
infor.createid = dt.Rows[i]["createid"].ToString();
infor.createtime = dt.Rows[i]["createtime"].ToString();
infor.Modifiesid = dt.Rows[i]["Modifiesid"].ToString();
infor.Modifiestime = dt.Rows[i]["Modifiestime"].ToString();
infor.brokeridm = dt.Rows[i]["brokeridm"].ToString();
informationtradingaccounts.Add(infor); //挨个装入
}
List<string> brokerS = (from val in informationtradingaccounts
select val.brokeridm).Distinct().ToList(); //linq语句,获取账户表的brokeridm的值,Distinct()去除重复值,ToList()将数组集合转成list集合
foreach(string broker in brokerS) //循环 brokerS列表数据,broker变量名
foreach(数据类型 变量名 in 数组名)
{
futurescompanyinformation futcom = new futurescompanyinformation();
futcom.brokerid = broker; //将broker赋给futcom.brokerid
List<Informationtradingaccount> tradaccount = informationtradingaccounts.FindAll(e => e.brokeridm == broker); //.FindAll获取所有数据,条件是e.brokeridm == broker这两个id相等的
if(tradaccount!=null) //如果这个tradaccount列表不等于空执行
{
futcom.brokername = tradaccount[0].brokername; //将futcom表中的名字赋给futcom表
for (int i = 0; i < tradaccount.Count; i++) //循环futcom表的长度
{
if (tradaccount[i].investorid != null) //如果账户id不为空
{
futcom.informationtradingaccounts.Add(tradaccount[i]); //挨个装入tradaccount表信息
}
}
}
futurescompanyinformations.Add(futcom); //将表信息添加到futurescompanyinformations表
}
}
string strjson = JsonConvert.SerializeObject(futurescompanyinformations); //将futurescompanyinformations表转成Json格式
GC.Collect(); //收集
return strjson; //返回出去
}
调用接口
$.ajax({
type: 'POST',
dataType: 'JSON',
contentType: 'application/json; charset=utf-8',
async: true,
cache: false,
url: urlstr + "View/LocalService/ServerMaintain.asmx/GetCompanyInformationGain",
//传递的参数
data: JSON.stringify({
}),
success: function (data) {
alert("数据接收成功")
MonthlyjsonObj = JSON.parse(data.d);
console.log(MonthlyjsonObj)
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("一般性网络问题,请求数据错误。");
//alert(XMLHttpRequest.status);
//alert(XMLHttpRequest.readyState);
//alert(textStatus);
}
});
结果集