I want to insert a JSON document to a varchar column in my MySQL database. I am developing my application in ASP.NET and c#. I use the following command:
MySqlCommand cmnd = new MySqlCommand("UPDATE sharding_conf SET config = \"" + json + "\" WHERE id = 1");
The problem is that the JSON document also contains the ” character so I get an error.
You have an error in your SQL syntax;
How can I insert a JSON document? – Just note that I intend to de-serialize it when I read the data back from the database and put it in an object.
Use parameterized queries in place of inline query. so using:
MySqlCommand cmnd = new MySqlCommand("UPDATE sharding_conf SET config = @config WHERE id = 1"); cmnd.Parameters.Add("@config",json);I don’t know if @ is used in my sql or ? but that’s how it can be fixed.