2015-09-04 234 views
0

我的Angular Web應用程序當前使用HTML5 localStorage對象將JSON對象序列化爲/從本地存儲器反序列化JSON對象。我們現在試圖保存並從SQL Server表中檢索這些相同的JSON對象。將JSON字符串保存到SQL Server數據庫的問題

但我有困難,因爲我嘗試使用以下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錯誤消息是:

消息102,15級,狀態1,第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表,所以我可以測試檢索功能。

回答

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 
+0

哦所以一個JavaScript str.replace()函數,例如?或str.replace(/'/ g,「\\'」); –

+0

我認爲這可以工作!只要你不讓它修改前導和尾隨單引號。 – rwking

相關問題