C# - 将 JSON 存储到 MySQL 数据库中 [重复]
Posted
技术标签:
【中文标题】C# - 将 JSON 存储到 MySQL 数据库中 [重复]【英文标题】:C# - Storing JSON into MySQL Database [duplicate] 【发布时间】:2021-05-17 07:34:21 【问题描述】:我正在使用 ASP NET
好的,所以我有这个 JSON,我通过 JSON.stringify
字符串化它看起来像这样:
"mean":[296,28.166036637951827,1003],"mode":[305,28.3565417425884,1007],"min":[117,20.5116820888701,991],"max":[322,29.7130352284327,1014],"range":[205,9.201353139562602,23],"variance":[2041.708,3.56,45.257],"std":[45.185,1.887,6.727],"allReadings":[["289","28.2724154175823","1003","2021-02-13T20:04:46.000Z"],["305","28.2724154175823","1014","2021-02-13T20:04:56.000Z"],["294","28.2724154175823","991","2021-02-13T20:05:06.000Z"],["304","28.3565417425884","1007","2021-02-13T20:05:16.000Z"],["305","28.3565417425884","996","2021-02-13T20:05:26.000Z"],["309","28.3565417425884","995","2021-02-13T20:05:37.000Z"],["322","28.3565417425884","1012","2021-02-13T20:05:47.000Z"],["301","28.3565417425884","995","2021-02-13T20:05:57.000Z"],["307","28.3565417425884","1007","2021-02-13T20:06:07.000Z"],["117","28.5250147637195","999","2021-02-13T20:06:17.000Z"],["273","20.5116820888701","1003","2021-02-13T20:06:28.000Z"],["314","28.8628633488818","1007","2021-02-13T20:06:38.000Z"],["296","28.7782864935014","1011","2021-02-13T20:06:48.000Z"],["320","28.6937866171596","1007","2021-02-13T20:06:58.000Z"],["319","28.6093629596379","1005","2021-02-13T20:07:08.000Z"],["307","29.7130352284327","1010","2021-02-13T20:07:19.000Z"],["312","28.947517946563","1007","2021-02-13T20:07:29.000Z"],["321","28.8628633488818","994","2021-02-13T20:07:39.000Z"],["318","28.6937866171596","1005","2021-02-13T20:07:49.000Z"]]
我正在尝试使用此 C# 代码将其保存到 mysql 数据库中:
report
= JSON
dt
= DateTime.Now.ToString()
public static void saveReport(string report, string dt)
string sql = $"INSERT INTO reports (report, date), VALUES ('report', 'dt')";
MySqlConnection con = new MySqlConnection(smartseed_con);
con.Open();
MySqlCommand cmd = new MySqlCommand(sql, con);
cmd.ExecuteNonQuery();
con.Close();
但是,它抛出了这个错误。
这就是我的桌子的设计方式
我急需一些帮助 :( 有人可以提出解决方案或更好的方法吗?我什至尝试在 C# 中执行 .Replace('"', '*')
,但这是行不通的。目标是将那个 " 字符替换为另一个字符这不会导致这个问题。:/
【问题讨论】:
使用参数cmd.Parameters.Add(...
并使用using
处理你的连接和命令对象
【参考方案1】:
使用SqlParameter
存储值。 sql 参数中的值将被视为值,不会被“解析”为查询的一部分。
using (var connection = new MySqlConnection("connectionString"))
using (var command = connection.CreateCommand())
var sql = $"INSERT INTO reports (report, date), VALUES (@report, @dt)";
var parameters = new[]
new MySqlParameter ParameterName = "@report", MySqlDbType = MySqlDbType.Varchar, Value = report ,
new MySqlParameter ParameterName = "@dt", MySqlDbType = MySqlDbType.Varchar, Value = dt ,
;
command.CommandText = sql;
command.Parameters.AddRange(parameters);
connection.Open();
command.ExecuteNonQuery();
注意: 不要将日期存储为 VARCHAR,MySql 支持 Date 和 Time 类型。
【讨论】:
MySqlParameter
你可以只做两次.Add
或者使用集合初始化器,它更简单
法比奥,什么是 SqlType?我需要Using
某个图书馆吗?
@Denise,这是MySqlDbType
,您不需要添加任何其他库。以上是关于C# - 将 JSON 存储到 MySQL 数据库中 [重复]的主要内容,如果未能解决你的问题,请参考以下文章
将两个单独的数据字段存储到一个数组中 c# Array Json from Class
如果将json格式数据存储到类似mysql这样的关系型数据库中,怎么查
无法使用 JSON 将 jqxScheduler 状态存储到 MySQL