1

我正在尝试使用 Visual Studio 2012 中的数据库项目为我的数据库播种。作为我的部署后脚本的公园,我有一个类似于以下内容的语句:

INSERT INTO SomeTable (SomeTextCol) Values (N'$(function(){});')

该列定义为NVARCHAR(MAX)

这会导致构建后事件失败。然而奇怪的是,如果我使用 SSMS 执行语句,它会成功。

这是由于数据库项目在后台使用 SQLCMD 造成的吗?

如何修复此错误(同时仍然能够将 jQuery 插入表中) - 更改列值对我来说不是一个选项,因为我不拥有架构。

4

1 回答 1

2

You are right, this is caused by sqlcmd for which $ is a special character. One solution to fix this would be to concatenate two strings:

INSERT INTO SomeTable (SomeTextCol) Values (N'$'+'(function(){});');

It is a little ugly, but it works. An alternative would be to use a different symbol for jQuery like its complete name:

INSERT INTO SomeTable (SomeTextCol) Values (N'jQuery(function(){});');

Or, if you have a lot of references to jQuery inside the function:

(function(JQ){
  JQ(
     function(){
     //other references to JQ
     }
  );
 }
)(jQuery);

This last solution has the added advantage that anyone defining $ or JQ anywhere else on the page won't break your code, as "your" JQ is defined in a closure visible only to your code.

于 2013-03-15T18:36:12.287 回答