本人刚接触json,请问怎么用json来获取sql数据库中的数据,然后再传回前台页面啊,谢谢了 ,分不多了,哪位好心人帮帮忙。

解决方案 »

  1.   

    两则之间的转换: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(); 


    }
      

  2.   

    参见
    jQuery Ajax 调用后台方法返回 DataSet 或者 DataTable 的例子
      

  3.   


    这样之后,json序列化获取的数据,该怎么实现那?麻烦你了