C# 包含“时出现语法错误”;或;一串

C# 包含“时出现语法错误”;或;一串,c#,sql,sql-server,irony,C#,Sql,Sql Server,Irony,我目前正在使用反讽作为我搜索过程的一部分(不是我的选择,我没有发言权:p),使用字母“或”后接空格和另一个单词,即“Orbit Gum”,这是一个问题。但是,如果你搜索像“Orbit”或简单的“或”这样的东西,它本身似乎工作得很好 发生的错误是 Syntax error near 'gum' in the full-text search condition 'orbit gum'. Description: An unhandled exception occurred during the

我目前正在使用反讽作为我搜索过程的一部分(不是我的选择,我没有发言权:p),使用字母“或”后接空格和另一个单词,即“Orbit Gum”,这是一个问题。但是,如果你搜索像“Orbit”或简单的“或”这样的东西,它本身似乎工作得很好

发生的错误是

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')

我自己用一个简单的正则表达式找到了解决这个问题的方法,结果是你可以简单地在字符串的开头和结尾加上引号,这样就不会抛出错误了

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

哦,天哪。为什么不把查询放在一个字符串中,而不是一打
AppendLine
子句中。在“普通”sql查询中,我会在最后一行使用
和nt.text,比如“%orbit gum%”
,这是一种非常可怕的方式,因为Irony在sql注入方面有很多问题,我不是100%了解搜索的工作原理,因为我实际上还没有编写大部分代码,只有修复问题字符的部分和谢天谢地的复仇,这看起来可能是解决这个问题的明智办法,我会尝试它并报告它的进展。@ HeloLoLD你可能想考虑编辑你的另一个问题,并关闭这一个,因为它实际上是同一个问题-道歉,如果不是,但我认为这两个都有效地归结为包含条款。