• Asp.net 将数据库里的记录转换成json


    以下是引用片段:

      using System;

      using System.Collections.Generic;

      using System.Text;

      using System.Data;

      using System.Data.SqlClient;

      namespace OTC.Utility

      ...{

      public sealed class JSONHelper

      ...{

      /**////

      /// 获取JSON字符串

      ///

      /// 值

      /// 数据表名

      ///

      public static string GetJSON(SqlDataReader drValue, string strTableName)

      ...{

      StringBuilder sb = new StringBuilder();

      sb.AppendLine("{");

      sb.AppendLine(" " + strTableName + ":{");

      sb.AppendLine(" records:[");

      try

      ...{

      while (drValue.Read())

      ...{

      sb.Append(" {");

      for (int i = 0; i < drValue.FieldCount; i++)

      ...{

      sb.AppendFormat(""":"",", drValue.GetName(i), drValue.GetValue(i));

      }

      sb.Remove(sb.ToString().LastIndexOf(’,’), 1);

      sb.AppendLine("},");

      }

      sb.Remove(sb.ToString().LastIndexOf(’,’), 1);

      }

      catch(Exception ex)

      ...{

      throw new Exception(ex.Message);

      }

      finally

      ...{

      drValue.Close();

      }

      sb.AppendLine(" ]");

      sb.AppendLine(" }");

      sb.AppendLine(" };");

      return sb.ToString();

      }

      }

      }

      接下来你只需要传一个SqlDataReader对象就可以了。

  • 相关阅读:
    Java反射机制DOME
    Contos7 装bcm4312无线网卡驱动
    windows8.1+centos7双系统(装完centos后无win8引导)
    request+response
    HTTP协议+servlet配置
    类加载和反射
    线程池+线程安全
    IO流之Properties(String键值对)+序列流+打印流+commons-IO(最终流程)
    IO流之字节流 +字符流 (复制文件)
    IO流
  • 原文地址:https://www.cnblogs.com/a13971240/p/1502216.html
Copyright © 2020-2023  润新知