您的位置:首页 > Web前端 > JavaScript

C# 当通讯数据比较多,需要分组发送,对DataSetToJson的重载

2012-01-10 08:50 489 查看
//数据集转为JSON数组主表专用 iMaxCount-最大通讯条数 iCom-第几次通讯 iLast--最后一次通讯条数,不是最后一次通讯传入0

public static string DataSetToJson(DataSet ds, string tblname,int iMaxCount,int iCom,int iLast)

{

string json = string.Empty;

try

{

if (ds.Tables.Count == 0)

throw new Exception("DataSet中Tables为0");

json = "{";

for (int i = 0; i < ds.Tables.Count; i++)

{

json += "'" + tblname + "'" + ":[";

//for (int j = 0; j < ds.Tables[i].Rows.Count; j++)

if (iLast == 0) //不是最后一次通讯的话这样处理

{

for (int j = iMaxCount * iCom; j <= (iCom + 1) * iMaxCount - 1; j++)

{

json += "{";

for (int k = 0; k < ds.Tables[i].Columns.Count; k++)

{

json += "'" + ds.Tables[i].Columns[k].ColumnName + "'" + ":'" + ds.Tables[i].Rows[j][k].ToString() + "'";

if (k != ds.Tables[i].Columns.Count - 1)

json += ",";

}

json += "}";

if (j != ds.Tables[i].Rows.Count - 1)

json += ",";

}

}

else //如果是最后一次通讯 则islast里面存的是剩余的未通讯的条数

{

for (int j = iMaxCount * iCom; j <= iCom * iMaxCount - 1 + iLast; j++)

{

json += "{";

for (int k = 0; k < ds.Tables[i].Columns.Count; k++)

{

json += "'" + ds.Tables[i].Columns[k].ColumnName + "'" + ":'" + ds.Tables[i].Rows[j][k].ToString() + "'";

if (k != ds.Tables[i].Columns.Count - 1)

json += ",";

}

json += "}";

if (j != ds.Tables[i].Rows.Count - 1)

json += ",";

}

}

json += "]";

if (i != ds.Tables.Count - 1)

json += ",";

}

json += "}";

}

catch (Exception ex)

{

throw new Exception(ex.Message);

}

return json;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: