Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)aaa
published in(发表于) 2013/12/18 7:53:46
Asp.net,将数据库里的记录转换成json_.net资料_编程技术

Asp.net 将数据库里的记录转换成json_.net资料_编程技术-你的首页-uuhomepage.com

  在前面我已经写了asp的版本,最近一个项目中正好需要用json来填充下拉框,所以写了一个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(""{0}":"{1}",", 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对象就可以了。





If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759