将JSON字符串保存到SQL Server数据库的问题

时间:2015-09-04 14:35:15

标签: sql-server json angularjs

我的Angular Web应用程序当前使用HTML5 localStorage对象将JSON对象序列化/反序列化到本地存储中。我们现在尝试将这些相同的JSON对象保存到SQL Server表中并从中检索这些JSON对象。

但是当我尝试使用以下SQL UPDATE保存JSON字符串时,我很难:

UPDATE [rz37883_SQLServer].[dbo].[RAGEDashboard]
SET 
  image = '{"widgets":[{"title":"Bar Chart","name":"chart_bar","style":{},"size":{"width":"50%","height":320},"dataModelOptions":{"title":{"text":"Bar Chart","position":"bottom"},"legend":{"visible":true,"position":"top"},"seriesDefaults":{"type":"bar","stack":false},"series":[{"field":"field1","name":"MTM"}],"dataSource":{"data":[{"field1":236151654.592439},{"field1":103612357.347808},{"field1":267066579.129913},{"field1":582355005.154486},{"field1":-9422699.958631}],"table":null},"valueAxis":{"labels":{"format":"{0:c}","rotation":-30},"line":{"visible":false},"axisCrossingValue":0},"categoryAxis":{"categories":["London","New York","Dubai","Paris","Stockholm"],"majorGridLines":{"visible":false},"labels":{"rotation":0,"margin":20}},"tooltip":{"visible":true,"template":"#= series.name #: #= kendo.format('{0:C0}', value) #"},"dimensions":[{"dimension":"BookingLocation","hierarchy":false,"id":0}],"riskMeasures":[{"name":"MTM","kri_group":"[MTM:MTM]","cube_vector":"MTM:MTM","aggreg_formula":"SUM(MTM:MTM)","id":0}]},"initImage":"images2/bar_chart.png","gadgetConfigured":true}]}'
 WHERE [RAGEDashboardConfig_userid] = 'bobmazzo1234' 
   AND id = 1441288790

SQL Server错误消息是:

  

Msg 102,Level 15,State 1,Line 3
  '{'。

附近的语法不正确

在角度指令代码中,item对象当前保存到localStorage,如下所示。

 save: function (widgets) {
   //'widgets' are mapped to the 'serialized' var using _.map()
   item = JSON.stringify(item);
   this.storage.setItem(this.id, item);
 }

我现在正在连接一个http请求,它将使用MS WebAPI调用c#API层。

这里的问题是我只是尝试手动更新SQL Server表,以便我可以测试检索功能。

1 个答案:

答案 0 :(得分:2)

我认为这是因为你在字符串中有几个单引号。你必须逃脱它们,具有讽刺意味的是,单引号是转义字符。

这是我做的一点修改:

原文:kendo.format('{0:C0}', value)

修改:kendo.format(''{0:C0}'', value)

尝试将字符串更改为此,看看它是否解决了问题:

UPDATE [rz37883_SQLServer].[dbo].[RAGEDashboard]
   SET 
      image = 
'{"widgets":[{"title":"Bar Chart","name":"chart_bar","style":{},"size":{"width":"50%","height":320},"dataModelOptions":{"title":{"text":"Bar Chart","position":"bottom"},"legend":{"visible":true,"position":"top"},"seriesDefaults":{"type":"bar","stack":false},"series":[{"field":"field1","name":"MTM"}],"dataSource":{"data":[{"field1":236151654.592439},{"field1":103612357.347808},{"field1":267066579.129913},{"field1":582355005.154486},{"field1":-9422699.958631}],"table":null},"valueAxis":{"labels":{"format":"{0:c}","rotation":-30},"line":{"visible":false},"axisCrossingValue":0},"categoryAxis":{"categories":["London","New York","Dubai","Paris","Stockholm"],"majorGridLines":{"visible":false},"labels":{"rotation":0,"margin":20}},"tooltip":{"visible":true,"template":"#= series.name #: #= kendo.format(''{0:C0}'', value) #"},"dimensions":[{"dimension":"BookingLocation","hierarchy":false,"id":0}],"riskMeasures":[{"name":"MTM","kri_group":"[MTM:MTM]","cube_vector":"MTM:MTM","aggreg_formula":"SUM(MTM:MTM)","id":0}]},"initImage":"images2/bar_chart.png","gadgetConfigured":true}]}'
WHERE [RAGEDashboardConfig_userid]='bobmazzo1234' and id = 1441288790