从DataReader中手动串行化JSON

时间:2025-03-19 10:35:55
void WriteDataReader(StringBuilder sb, IDataReader reader)
{
if (reader == null || reader.FieldCount == )
{
sb.Append("null");
return;
} int rowCount = ; sb.Append("{\"Rows\":[\r\n"); while (reader.Read())
{
sb.Append("{"); for (int i = ; i < reader.FieldCount; i++)
{
sb.Append("\"" + reader.GetName(i) + "\":") ;
this.WriteValue(sb,reader[i]);
sb.Append(",");
if (this.FormatJsonOutput)
sb.Append("\r\n");
}
// strip off trailing comma
if (reader.FieldCount > )
this.StripComma(sb); sb.Append("},");
if (this.FormatJsonOutput)
sb.Append("\r\n"); rowCount++;
} // remove trailing comma
if (rowCount > )
this.StripComma(sb); sb.Append("]}");
}