【发布时间】:2021-04-13 06:51:35
【问题描述】:
我目前正在开发一个程序,该程序应将 sql 数据库中的信息以 json 格式 格式放置,以便用于 amCharts。我得到一个 System.Format.Exception 错误,我无法修复它。我还在学习使用 C# Asp.Net。 这是我的代码:
// Get the connection string
string connStr = ConfigurationManager.ConnectionStrings["CRMMonitor_SQL"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connStr))
{
// Establish the connection with the database
conn.Open();
// Construct and execute SQL query which would return the total amount for each year
SqlCommand query = new SqlCommand(chartSQL, conn);
// Begin iterating through the result set
SqlDataReader rst = query.ExecuteReader();
while (rst.Read())
{
jsonStr.AppendFormat("{\"label\": \"{0}\", \"value\": \"{1}\"}", @rst[0].ToString(), @rst[1].ToString());
}
输出应如下所示:
"data": [{
"label": "label1",
"value": 501
}, {
"label": "label2",
"value": 301
}, {
"label": "label3",
"value": 128
}]
【问题讨论】:
-
这很难看。为什么不用 Newtonsoft 序列化 BL 对象?
标签: c# asp.net json syntax-error amcharts