日期:2014-05-17 浏览次数:20785 次
private string ConvertToJson(DataSet ds)
{
StringBuilder retVal = new StringBuilder();
int TableCount = 0, RowCount = 0, ColumnCount = 0;
retVal.Append("{");
foreach (DataTable dt in ds.Tables)
{
TableCount++;
retVal.AppendFormat("{0}:[", dt.TableName);
foreach (DataRow row in dt.Rows)
{
RowCount++; retVal.Append("{");
foreach (DataColumn column in dt.Columns)
{
ColumnCount++; retVal.AppendFormat("{0}:\"{1}\"{2}", column.ColumnName, row[column].ToString().Trim().Replace("'", "\\'").Replace("\\r", "<br/>").Replace("\\n", "<br/>").Replace(System.Environment.NewLine, "<br/>"), ColumnCount == dt.Columns.Count ? "" : ",");
} ColumnCount = 0; retVal.Append("}"); retVal.AppendFormat("{0}", RowCount == dt.Rows.Count ? "" : ",");
}
RowCount = 0;
retVal.Append("]");
retVal.AppendFormat(TableCount == ds.Tables.Count ? "" : ",");
}
retVal.Append("}");
return retVal.ToString();
}