参数化SQL Server中的全文查询
我使用SQL Server全文功能有问题。我正在将一些论坛软件转换为使用全文搜索,并且设置了所有内容。我的问题与全文查询有关。当我在SQL Server Management Studio中使用包含的谓词来定位我的搜索结果时,我设计了一些查询,这些查询是根据所需运行的,例如:
Select ....
From .....
WHERE Contains(p.Message,'" dog food "' ) ......
这是可以运行的,但是如何在准备好的语句中参数化它?理想情况下,我希望能够使用“ were”子句类似的查询来运行查询:
Select ....
From .....
WHERE Contains(p.Message,'" @SearchTerm "' ) ...
甚至是
WHERE Contains(p.Message,'"@SearchTerm" Near "@OtherSearchTerm" ) ...
由于双引号和所有内容而行不通的。我可以在代码中动态构建搜索术语,但是出于安全原因,我确实需要将参数用于所有用户输入。我看了一个数十亿个Google结果,试图找到解决方案,但不能(当然,这一定会发生,或者我在这里错过了一些很明显的东西和/或不可能)。有什么想法吗?
I have a problem using the sql server full text feature. I'm converting some forum software to use the full text search and I have everything setup and working. My problems are related to full text queries. I have designed a few queries which run as desired when I test them in sql server management studio using the CONTAINS predicate to locate my search results eg:
Select ....
From .....
WHERE Contains(p.Message,'" dog food "' ) ......
So this runs fine but how can I parameterize this in a prepared statement? Ideally I would like to be able to run a query with a where clause like:
Select ....
From .....
WHERE Contains(p.Message,'" @SearchTerm "' ) ...
or even
WHERE Contains(p.Message,'"@SearchTerm" Near "@OtherSearchTerm" ) ...
But this approach doesn't work because of the double quotes and all. I could build the search term up dynamically in the code but I really need to be using parameters for all user input for security reasons. I have looked at a zillion google results trying to find a solution but can't(Surely this must happen everyone or am I missing something really obvious here and/or it's not possible ). Any Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建一个带有参数的存储过程,例如:
然后从代码中调用它。
如何:通过使用Visual C#.net
Create a stored procedure with parameters, like:
Then call it from your code.
HOW TO: Call SQL Server Stored Procedures in ASP.NET by Using Visual C# .NET
字符串串联怎么样?
How about string concatenation?
该答案使用Enterprise Library 5.0在VB.NET中演示了一个参数化的SQL Server全文搜索;并进一步显示了每种“对象类型”(想人,地点和事物)返回十行。
给定下表和全文索引:
代码:
This answer demonstrates a parameterized SQL Server Full-Text Search in VB.NET using the Enterprise Library 5.0; and further shows returning ten rows for each "object type" (think people, places and things).
Given the following table and full-text index:
Code: