//数据集转为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;
}
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;
}