新聞中心
數(shù)據(jù)庫里的記錄與json之間轉(zhuǎn)換。代碼如下:

為江北等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及江北網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站設(shè)計、網(wǎng)站制作、江北網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace OTC.Utility
...{
public sealed class JSONHelper
...{
/**////
/// 獲取JSON字符串
///
/// 值
/// 數(shù)據(jù)表名
///
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對象就可以了。
到這里數(shù)據(jù)庫里的記錄與json之間的轉(zhuǎn)換就可以實現(xiàn)了,通過上文中的代碼,數(shù)據(jù)庫里的記錄與json之間的轉(zhuǎn)換就容易很多了,二者之間的轉(zhuǎn)換的介紹主要是以代碼的形式展現(xiàn)在大家面前,對于沒有很多數(shù)據(jù)庫知識的初學(xué)者來說可能比較不容易理解,希望大家能夠深入其中去學(xué)習(xí)。
【編輯推薦】
- 詳解Oracle數(shù)據(jù)庫備份不同的恢復(fù)特性
- Access數(shù)據(jù)庫成功導(dǎo)入Oracle庫方法
- Oracle數(shù)據(jù)庫中使用外部表的心得
網(wǎng)站欄目:數(shù)據(jù)庫里的記錄與json之間如何轉(zhuǎn)換
本文鏈接:http://fisionsoft.com.cn/article/dpgdcci.html


咨詢
建站咨詢
