从DataReader中手动串行化JSON

void WriteDataReader(StringBuilder sb, IDataReader reader)
{
    if (reader == null || reader.FieldCount == 0)
    {
        sb.Append("null");
        return;
    }

    int rowCount = 0;

    sb.Append("{"Rows":[
");

    while (reader.Read())
    {
        sb.Append("{");
        
        for (int i = 0; i < reader.FieldCount; i++)
        {
            sb.Append(""" + reader.GetName(i) + "":") ;
            this.WriteValue(sb,reader[i]);
            sb.Append(",");
            if (this.FormatJsonOutput)
                sb.Append("
");                    
        }
        // strip off trailing comma
        if (reader.FieldCount > 0)
            this.StripComma(sb);

        sb.Append("},");
        if (this.FormatJsonOutput)
            sb.Append("
");
        
        rowCount++;
    }

    // remove trailing comma
    if (rowCount > 0)
        this.StripComma(sb);

    sb.Append("]}");
}
原文地址:https://www.cnblogs.com/sunxuchu/p/5567626.html