2012-07-27 102 views
0

我目前使用Irony作为我的搜索过程的一部分(不是我的选择,我没有发言权:P),并且存在使用字母组合的问题“或”后跟一个空格和另一个词,即IE“Orbit Gum”,但是如果你自己搜索诸如“orbit”或简单地“or”之类的东西,它似乎工作得很好。在字符串中包含“或”时出现语法错误

时发生的错误是

Syntax error near 'gum' in the full-text search condition 'orbit gum'. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Syntax error near 'gum' in the full-text search condition 'orbit gum'. 

正在使用的代码来生成这部分是

 //Union with the full text search 
     if (!string.IsNullOrEmpty(fTextSearch)) 
      { 
       sql.AppendLine("UNION"); 
       sql.AppendLine(commonClause); 
       sql.AppendLine(string.Format("AND CONTAINS(nt.text, '{0}', LANGUAGE 'English')", fTextSearch)); 
      } 

是据我可以看到导致该问题的实际查询是这样的行:

AND CONTAINS(nt.text, 'orbit gum', LANGUAGE 'English') 
+2

[也许你需要用双引号(http://stackoverflow.com/questions/9435119/syntax-error-near-of-in-the-full-text-search-condition-control-哦) – V4Vendetta 2012-07-27 12:22:05

+0

哦,天啊。你为什么不把查询放在一个字符串中而不是十几个'AppendLine'子句中。 – CodesInChaos 2012-07-27 12:24:02

+0

在一个“正常”的sql查询中,我会在最后一行使用'AND nt.text LIKE'%orbit gum%'' – Alex 2012-07-27 12:24:04

回答

0

我已经找到了解决方案,我自己使用一个简单的正则表达式,它变成了您可以简单地将引号添加到字符串的开始和结尾,并停止引发错误。

fTextSearch = Regex.Replace(fTextSearch, ".*(or|and|etc).*", "\"$&\""); 
相关问题