public string toJSON(SqlDataReader o)
{
StringBuilder s = new StringBuilder();
s.Append("[");
if (o.HasRows)
while (o.Read())
s.Append("{" + '"' + "Id" + '"' + ":" + o["Id"] + ", "
+ '"' + "CN" + '"' + ":" + o["CatName"] + ", "
+ '"' + "Ord" + '"' + ":" + o["Ord"] + ","
+ '"' + "Icon" + '"' + ":" + o["Icon"] + "}, ");
s.Remove(s.Length - 2, 2);
s.Append("]");
o.Close();
return s.ToString();
}
我在这里使用自己的函数进行序列化。我需要知道这是一个好方法还是我应该使用另一个。顺便说一下,我曾尝试使用JavaScriptSerializer
,但这不适用于SqlDataReader。 thanx
答案 0 :(得分:52)
如果你想要一些可以转换为任意JSON的东西,你可以通过将它序列化为一个Dictionary(Of string,object)进行转换:
public IEnumerable<Dictionary<string, object>> Serialize(SqlDataReader reader)
{
var results = new List<Dictionary<string, object>>();
var cols = new List<string>();
for (var i = 0; i < reader.FieldCount; i++)
cols.Add(reader.GetName(i));
while (reader.Read())
results.Add(SerializeRow(cols, reader));
return results;
}
private Dictionary<string, object> SerializeRow(IEnumerable<string> cols,
SqlDataReader reader) {
var result = new Dictionary<string, object>();
foreach (var col in cols)
result.Add(col, reader[col]);
return result;
}
然后使用NewtonSoft.Json JsonConvert对象获取您的JSON:
var r = Serialize(reader);
string json = JsonConvert.SerializeObject(r, Formatting.Indented);
<强>更新强> 如果您只是想使用内置方法,并且碰巧使用MVC,则可以在新序列化中使用内置的Json辅助方法:
JsonResult Index(int id) {
var r = Serialize(reader);
return Json(r, JsonRequestBehavior.AllowGet);
}
答案 1 :(得分:14)
另一种选择是使用James Newton-King优秀的JSON.NET库 - http://www.newtonsoft.com/json
以下是一个快速示例,介绍如何使用它构建集合,然后将其作为JSON序列化字符串输出:
using Newtonsoft.Json;
class Program
{
static void Main(string[] args)
{
ArrayList objs = new ArrayList();
//get the data reader, etc.
while(o.Read())
{
objs.Add(new
{
Id = o["Id"],
CN = o["CatName"],
Ord = o["Ord"],
Icon = o["Icon"]
});
}
//clean up datareader
Console.WriteLine(JsonConvert.SerializeObject(objs));
Console.ReadLine();
}
}
您可以通过将SqlDataReader的每一行读入一个匿名对象,然后使用JSON.NET将其序列化为字符串来对循环执行相同的操作。
希望这有帮助!
答案 2 :(得分:14)
我遇到数据读取器返回的行数在内存消耗方面可能会出现问题的用例。以下代码在流上使用JsonWriter(来自JSON.NET)。人们当然可以辩论巨大的JSON文档的效用,但有时我们的用例由其他人决定: - )
一些注意事项:
代码:
var stream = ... // In my case, a FileStream or HttpResponse stream
using (var writer = new JsonTextWriter(new StreamWriter(stream)))
{
writer.WriteStartObject();
do
{
int row = 0;
string firstColumn = null;
while (await reader.ReadAsync())
{
if (row++ == 0)
{
firstColumn = reader.GetName(0);
writer.WritePropertyName(string.Format("{0}Collection", firstColumn));
writer.WriteStartArray();
}
writer.WriteStartObject();
for (int i = 0; i < reader.FieldCount; i++)
{
if (!reader.IsDBNull(i)) {
writer.WritePropertyName(reader.GetName(i));
writer.WriteValue(reader.GetValue(i));
}
}
writer.WriteEndObject();
}
writer.WriteEndArray();
} while (await reader.NextResultAsync());
writer.WriteEndObject();
}
异构输出的一个例子是:
{
"ContactCollection": {
"ContactItem": [{
"ContactID": "1",
"Contact": "Testing",
},
{
"ContactID": "2",
"Contact": "Smith, John",
},
{
"ContactID": "4",
"Contact": "Smith, Jane",
}
],
"MessageItem": [{
"MessageID": "56563",
"Message": "Contract Review Changed",
},
{
"MessageID": "56564",
"Message": " Changed",
},
{
"MessageID": "56565",
"Message": "Contract Review - Estimated Completion Added.",
}
]
}
}
参考:
答案 3 :(得分:7)
试试这个:
o = cmd.ExecuteReader();
var dataQuery = from d in o.Cast<DbDataRecord>()
select new
{
Id = (String)d["Id"],
CN = (String)d["CatName"],
Ord = (String)d["Ord"],
Icon = (String)d["Icon"]
};
var data = dataQuery.ToArray();
JavaScriptSerializer serializer = new JavaScriptSerializer();
String jsonData = serializer.Serialize(data);
答案 4 :(得分:6)
这应该做的工作
private String sqlDatoToJson(SqlDataReader dataReader)
{
var dataTable = new DataTable();
dataTable.Load(dataReader);
string JSONString = string.Empty;
JSONString = JsonConvert.SerializeObject(dataTable);
return JSONString;
}
答案 5 :(得分:4)
我使用此代码,基于Jonathan's answer:
private IEnumerable<Dictionary<string, object>> ConvertToDictionary(IDataReader reader)
{
var columns = new List<string>();
var rows = new List<Dictionary<string, object>>();
for (var i = 0; i < reader.FieldCount; i++)
{
columns.Add(reader.GetName(i));
}
while (reader.Read())
{
rows.Add(columns.ToDictionary(column => column, column => reader[column]));
}
return rows;
}
然后:
var rows = this.ConvertToDictionary(reader);
return JsonConvert.SerializeObject(rows, Formatting.Indented);
答案 6 :(得分:2)
自SQL Server 2016起,Microsoft将此功能嵌入到sql查询中。您可以在查询结束时使用FOR JSON
关键字来实现此目的。
select * from table_example where somecolumn = somecondition FOR JSON AUTO
有关更多详细信息和示例,请查看此官方文档Format JSON Output Automatically with AUTO Mode (SQL Server)
Here是Microsoft的C#代码示例,用于从SQL查询获取JSON字符串。
var queryWithForJson = "SELECT ... FOR JSON";
var conn = new SqlConnection("<connection string>");
var cmd = new SqlCommand(queryWithForJson, conn);
conn.Open();
var jsonResult = new StringBuilder();
var reader = cmd.ExecuteReader();
if (!reader.HasRows)
{
jsonResult.Append("[]");
}
else
{
while (reader.Read())
{
jsonResult.Append(reader.GetValue(0).ToString());
}
}
警告:此解决方案仅对 SQL SERVER 2016 及更高版本有效。
答案 7 :(得分:1)
这不是那么难。当我想将搜索结果作为JSON返回到网页时,这就是我所做的。
首先,有一个像这样的课程
public class SearchResult
{
public string model_no { get; set; }
public string result_text { get; set; }
public string url { get; set; }
public string image_url { get; set; }
}
然后输入以下代码。
string sql_text = "select * from product_master where model_no like @search_string and active=1";
SqlConnection connection = new SqlConnection(sql_constr);
SqlCommand cmd = new SqlCommand(sql_text, connection);
cmd.Parameters.AddWithValue("@search_string", "%" + search_string + "%");
connection.Open();
SqlDataReader rdr = cmd.ExecuteReader();
List<SearchResult> searchresults = new List<SearchResult>();
while (rdr.Read())
{
SearchResult sr = new SearchResult();
sr.model_no = rdr["model_no"].ToString();
sr.result_text = rdr["product_name"].ToString();
sr.url = rdr["url_key"].ToString();
searchresults.Add(sr);
}
connection.Close();
//build json result
return Json(searchresults, JsonRequestBehavior.AllowGet);
这对我很有用..
答案 8 :(得分:1)
这是为了增强Chandu使用查询语法的Linq答案(来自... select ...)。如果您更喜欢方法语法,这是您的答案。
drdr = cmd.ExecuteReader();
Record[] recs = drdr.Cast<DbDataRecord>().Select( data=>new Record{
GraphID=(drdr.IsDBNull(0) ? "" : (string)data["LabelX"])
, XAxis=(drdr.IsDBNull(1) ? "1999-09-09 00:00:00" : Convert.ToDateTime(data["XDate"]).ToString("yyyy-MM-dd HH:mm:ss"))
, YVal=(drdr.IsDBNull(2) ? 0 : int.Parse(data["YFreq"].ToString()))
}).ToArray();
MemoryStream mem = new MemoryStream();
DataContractJsonSerializer szr = new DataContractJsonSerializer(typeof(Record[]));
szr.WriteObject(mem, recs);
String jsonData = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);
希望它对某人有帮助。
答案 9 :(得分:1)
继Jonathan's答案之后,我在ASP.NET Core中有类似的要求将SQLDataReader的结果转换为JSON字符串或结果对象, 所以我为它创建了一个扩展方法:
public static class MyExtensions
{
public async static Task<string> toJSON(this SqlDataReader reader)
{
var results = await reader.GetSerialized();
return JsonConvert.SerializeObject(results, Formatting.Indented);
}
public async static Task<IEnumerable<Dictionary<string, object>>> GetSerialized(this SqlDataReader reader)
{
var results = new List<Dictionary<string, object>>();
var cols = new List<string>();
for (var i = 0; i < reader.FieldCount; i++)
cols.Add(reader.GetName(i));
while (await reader.ReadAsync())
results.Add(SerializeRow(cols, reader));
return results;
}
private static Dictionary<string, object> SerializeRow(IEnumerable<string> cols,
SqlDataReader reader)
{
var result = new Dictionary<string, object>();
foreach (var col in cols)
result.Add(col, reader[col]);
return result;
}
}
&安培;根据我的要求使用它:
var result = await reader.GetSerialized(); //to get the result object
或
string strResult = await reader.toJSON(); //to get the result string
我创建了一个异步方法,因为在从数据库完成读取之前我还有其他一些事情要做。
答案 10 :(得分:1)
使用Cinchoo ETL-一个开放源代码库,您只需几行代码即可轻松将SqlDataReader导出为JSON
string connectionstring = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Northwind;Integrated Security=True";
StringBuilder sb = new StringBuilder();
using (var conn = new SqlConnection(connectionstring))
{
conn.Open();
var comm = new SqlCommand("SELECT top 2 * FROM Customers", conn);
using (var parser = new ChoJSONWriter(sb))
parser.Write(comm.ExecuteReader());
}
Console.WriteLine(sb.ToString());
输出:
[
{
"CustomerID": "ALFKI",
"CompanyName": "Alfreds Futterkiste",
"ContactName": "Maria Anders",
"ContactTitle": "Sales Representative",
"Address": "Obere Str. 57",
"City": "Berlin",
"Region": {},
"PostalCode": "12209",
"Country": "Germany",
"Phone": "030-0074321",
"Fax": "030-0076545"
},
{
"CustomerID": "ANATR",
"CompanyName": "Ana Trujillo Emparedados y helados",
"ContactName": "Ana Trujillo",
"ContactTitle": "Owner",
"Address": "Avda. de la Constitución 2222",
"City": "México D.F.",
"Region": {},
"PostalCode": "05021",
"Country": "Mexico",
"Phone": "(5) 555-4729",
"Fax": "(5) 555-3745"
}
]
答案 11 :(得分:0)
我做了以下方法,它将任何DataReader转换为JSON,但仅用于单深度序列化:
您应该传递阅读器,并将列名称作为字符串数组,例如:
String [] columns = {"CustomerID", "CustomerName", "CustomerDOB"};
然后调用方法
public static String json_encode(IDataReader reader, String[] columns)
{
int length = columns.Length;
String res = "{";
while (reader.Read())
{
res += "{";
for (int i = 0; i < length; i++)
{
res += "\"" + columns[i] + "\":\"" + reader[columns[i]].ToString() + "\"";
if (i < length - 1)
res += ",";
}
res += "}";
}
res += "}";
return res;
}
答案 12 :(得分:0)
将参考:System.Web.Extensions
添加到项目中
然后
using System.Web.Script.Serialization;
在c#代码中,您可以使用write:
var json = new JavaScriptSerializer().Serialize(obj);