C#查询datatable接收数据集并转换为json返回
StringBuilder commandText = new StringBuilder();
commandText.Append("select clientcode,coperator, skdate,usefund,skway,skprice from ReceiptLog");
commandText.Append(" WHERE clientcode = '"+Custcode+"'");
SysDBModel.CodeValue Codeobj = new SysDBModel.CodeValue();
DataTable CodeTable = new DataTable();
Codeobj.ExecuteQuery(commandText.ToString(), CodeTable);
var jsonString = new StringBuilder();
if (CodeTable.Rows.Count > 0)
{
jsonString.Append("[");
for (int i = 0; i < CodeTable.Rows.Count; i++)
{
jsonString.Append("{");
for (int j = 0; j < CodeTable.Columns.Count; j++)
{
if (j < CodeTable.Columns.Count - 1)
{
jsonString.Append("\"" + CodeTable.Columns[j].ColumnName.ToString()
+ "\":" + "\""
+ CodeTable.Rows[i][j].ToString() + "\",");
}
else if (j == CodeTable.Columns.Count - 1)
{
jsonString.Append("\"" + CodeTable.Columns[j].ColumnName.ToString()
+ "\":" + "\""
+ CodeTable.Rows[i][j].ToString() + "\"");
}
}
if (i == CodeTable.Rows.Count - 1)
{
jsonString.Append("}");
}
else
{
jsonString.Append("},");
}
}
jsonString.Append("]");
}
return jsonString.ToString();
return "OK";