UPDATE SQL 不同环境执行结果不一样

背景:1.前台:JQUERY 提交数据

           2.后台:OWIN C#  处理接收数据

   3.数据库: postgresql

============================================

问题描述:  后台接收到前台的数据,拼装一个 SQL 语句

update wbs_data 
set work_data='{"proName":"科研GOOD","proCode":"科研项目001","unitProName":"No1","drawingNo":"734116","proAddress":"大庆",
                 "partsComponents":"让胡路锅底",
                 "checkContent":"


这是一测试图片\"\"

", "checkOpinion":"检查意见:
1.好
2.很好
3.非 常好","materialDocs":"原材料质量文件:
1.测试文件1
2.测试文件2", "monitor":"","deputy":"","superEngineer":"","techLeader":"","date1":"","date2":"","date3":""}
',work_state=1 where pwbs_id=124

 

     我在后台C# 用 Npgsql执行时总是报错:invalid input syntax for type  json

public static string executeSQL(string strSQL)
        {
            Console.WriteLine("{0}", strSQL);
            string returnValue = string.Empty;
            NpgsqlConnection connection = new NpgsqlConnection(getConnectionString());
            NpgsqlCommand command = new NpgsqlCommand();
            command.Connection = connection;
            command.CommandText = strSQL;
            try
            {
                connection.Open();
                command.ExecuteNonQuery();
                //returnValue = "{\"SUCCESS\":\"执行操作:'" + strSQL + "'成功!\"}";
                Console.WriteLine("{0}","--------------------you are lucky----------------");
                returnValue = "{\"SUCCESS\":\"操作成功!\"}";

            }
            catch (Exception err)
            {
                returnValue = "{\"ERROR\":\"发生以下错误:" + err.Message.ToString() + "\"}";
            }
            finally
            {
                connection.Close();
            }
            return returnValue;
        }

 

奇怪的是,当我把这个拼装的SQL语句放在后台服务上PSQL 运行时,居然还通过了!

why??????

========================================================

经过测试发现:

我在获取生成JSON 串时,有个replace(/\s/g, ' ')(JS代码中)没有加,而当我加上时就不再有这个问题

然而,另一位大神给出最本质的原因:

我在PSQL 中执行SQL ,实际是做了强制转化,而我在C# 中运行是没有经过处理的应该把SQL变成这样:

update tablename set column = value::json where id = _id;

即我上面的代码应该转化为:

update wbs_data 
set work_data='{"proName":"科研GOOD","proCode":"科研项目001","unitProName":"No1","drawingNo":"734116","proAddress":"大庆",
                 "partsComponents":"让胡路锅底",
                 "checkContent":"


这是一测试图片\"\"

", "checkOpinion":"检查意见:
1.好
2.很好
3.非 常好","materialDocs":"原材料质量文件:
1.测试文件1
2.测试文件2", "monitor":"","deputy":"","superEngineer":"","techLeader":"","date1":"","date2":"","date3":""}
'::json,work_state=1 where pwbs_id=124

 

你可能感兴趣的:(UPDATE SQL 不同环境执行结果不一样)