<script type="text/javascript">google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("<s"+"cript type='text/javascript' s"+"rc='http://pagead2.googlesyndication.com/pagead/show_ads"+"."+"js'></scr"+"ipt>");</script>
在前面我已经写了asp的版本,最近一个项目中正好需要用json来填充下拉框,所以写了一个asp.net的版本,代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace OTC.Utility

...{
public sealed class JSONHelper

...{

/**//// <summary>
/// 获取JSON字符串
/// </summary>
/// <param name="drValue">值</param>
/// <param name="strTableName">数据表名</param>
/// <returns></returns>
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();
}
}
}
<<script type="text/javascript">google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("<s"+"cript type='text/javascript' s"+"rc='http://pagead2.googlesyndication.com/pagead/show_ads"+"."+"js'></scr"+"ipt>");</script>"");
接下来你只需要传一个SqlDataReader对象就可以了。
在前面我已经写了asp的版本,最近一个项目中正好需要用json来填充下拉框,所以写了一个asp.net的版本,代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace OTC.Utility
...{
public sealed class JSONHelper
...{
/**//// <summary>
/// 获取JSON字符串
/// </summary>
/// <param name="drValue">值</param>
/// <param name="strTableName">数据表名</param>
/// <returns></returns>
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();
}
}
}<<script type="text/javascript">google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("<s"+"cript type='text/javascript' s"+"rc='http://pagead2.googlesyndication.com/pagead/show_ads"+"."+"js'></scr"+"ipt>");</script>"");
接下来你只需要传一个SqlDataReader对象就可以了。
本文介绍了一种使用ASP.NET从SqlDataReader对象生成JSON格式数据的方法。通过提供的代码示例,可以将数据库查询结果转换为JSON格式,适用于填充下拉框等应用场景。
253

被折叠的 条评论
为什么被折叠?



