zoukankan      html  css  js  c++  java
  • 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对象就可以了。

  • 相关阅读:
    刷新页面的问题
    JS属性
    1-4_基本概念_进程间通信及同步
    1-3_基本概念_程序&进程
    1-2_基本概念_用户和组&目录、链接及文件
    1-1_基本概念_内核&shell
    d 操作系统概述
    对比生成测试算法用例
    简单的排序算法:冒泡排序法
    简单的排序算法:插入排序法
  • 原文地址:https://www.cnblogs.com/a13971240/p/1502216.html
Copyright © 2011-2022 走看看