[英]SQL query with contains / isabout works perfect in SQL Server Management Studio, but it does not in code behind
I would like to ask what I am doing wrong.我想问我做错了什么。 I have this query that works perfectly in SQL Server Management Studio:我有这个查询在 SQL Server Management Studio 中完美运行:
select *
from products, containstable(products, prod, 'isabout("laptop anybrand" weight (1))') as resultado
where products.ImageIDS = resultado.[Key]
order by resultado.[Rank] desc
But when I put it into the code-behind, it does not work, and is like this:但是当我将它放入代码隐藏时,它不起作用,并且是这样的:
select *
from products, containstable(products, prod,'isabout(" + searcht.Text + " weight (1))') as resultado
where products.ImageIDS = resultado.[Key]
order by resultado.[Rank] desc
The debugger does not send any error, but it does not work, next I am posting the code that I am using the whole query:调试器没有发送任何错误,但它不起作用,接下来我发布我正在使用整个查询的代码:
Private Sub BindGrid2()
Dim use As SqlConnection
use = New SqlConnection("Data Source=DESKTOP-50SIV8B\SQLEXPRESS;Initial Catalog=cupon2; User ID=XXXX; password=XXXX")
use.Open()
Dim queryy As String = "select * from products, containstable(products, prod,'isabout(" + searcht.Text + " weight (1))') as resultado where products.ImageIDS = resultado.[Key] order by resultado.[Rank] desc"
Dim DAA As SqlDataAdapter = New SqlDataAdapter(queryy, use)
Dim DTT As New Data.DataTable
searchid.Text = ""
subc.Items.Clear()
ids.Items.Clear()
DAA.Fill(DTT)
gv2.DataSource = DTT
gv2.DataBind()
use.Close()
End Sub
Hope anyone can help me what is wrong.希望任何人都可以帮助我有什么问题。
Thanks!谢谢!
Look at this string literal from the SSMS query:查看 SSMS 查询中的此字符串文字:
'isabout("laptop anybrand" weight (1))'
Note the literal itself contains double-quotes around the search key.请注意,文字本身包含搜索键周围的双引号。
Now look at the VB excerpt:现在看一下VB的摘录:
'isabout(" + searcht.Text + " weight (1))'
There are double quotes here, but they are not part of the final query, and there's nothing in the code to make sure they are added.这里有双引号,但它们不是最终查询的一部分,并且代码中没有任何内容可以确保添加它们。
What you should do is change the SSMS version like this:您应该做的是像这样更改 SSMS 版本:
DECLARE @SearchKey nvarchar(50) = N'laptop anybrand'
SELECT ... containstable(products, prod, N'isabout("' + @SearchKey + '" weight (1))')
You may have to play with this some in SSMS to get it working again, but when it is do the same thing in VB.您可能必须在 SSMS 中使用它来让它再次工作,但是当它在 VB 中做同样的事情时。 Note the comments I added:请注意我添加的评论:
'Move all your database code into it's own module!
Friend Module DB
Private connectString As String = "Data Source=DESKTOP-50SIV8B\SQLEXPRESS;Initial Catalog=cupon2; User ID=XXXX; password=XXXX"
Public Function SearchProducts(searchKey As String) As DataTable
'String literals can be more than one line now
Dim query As String = "
SELECT *
FROM products p
INNER JOIN containstable(products, prod,'isabout("" + @SearchKey + "" weight (1))') as resultado
ON p.ImageIDS = resultado.[Key]
ORDER BY resultado.[Rank] DESC"
Dim result As New DataTable()
'Using blocks guarantee the connection is closed, **even if an exception is thrown!**
Using cn As New SqlConnection(connectString), _
cmd As New SqlCommand(query, cn), _
da As New SqlDataAdapter(cmd)
'Parameterized queries stop lots of bugs, hackers, and are **faster**
cmd.Parameters.Add("@SearchKey", SqlDbType.NVarChar, 50).Value = searchKey
da.Fill(result)
End Using
Return result.Tables(0)
End Function
End Module
Private Sub BindGrid2()
searchid.Text = ""
subc.Items.Clear()
ids.Items.Clear()
gv2.DataSource = DB.SearchProducts(searcht.Text)
gv2.DataBind()
End Sub
Thanks for your answer, it is complete but, I found an easier solution.感谢您的回答,它是完整的,但我找到了一个更简单的解决方案。 Which is:这是:
String.Format("select * from products, freetexttable(products, prod,'isabout({0}{1}{2} weight (1))') as resultado where products.ImageIDS = resultado.[Key] order by resultado.[Rank] desc", """", searcht.Text.Trim(), """")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.