![](/img/trans.png)
[英]Why does this SQL code not work in Microsoft Access but works in SQL Server Management Studio?
[英]SQL query with contains / isabout works perfect in SQL Server Management Studio, but it does not in code behind
我想问我做错了什么。 我有这个查询在 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
但是当我将它放入代码隐藏时,它不起作用,并且是这样的:
select *
from products, containstable(products, prod,'isabout(" + searcht.Text + " weight (1))') as resultado
where products.ImageIDS = resultado.[Key]
order by resultado.[Rank] desc
调试器没有发送任何错误,但它不起作用,接下来我发布我正在使用整个查询的代码:
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
希望任何人都可以帮助我有什么问题。
谢谢!
查看 SSMS 查询中的此字符串文字:
'isabout("laptop anybrand" weight (1))'
请注意,文字本身包含搜索键周围的双引号。
现在看一下VB的摘录:
'isabout(" + searcht.Text + " weight (1))'
这里有双引号,但它们不是最终查询的一部分,并且代码中没有任何内容可以确保添加它们。
您应该做的是像这样更改 SSMS 版本:
DECLARE @SearchKey nvarchar(50) = N'laptop anybrand'
SELECT ... containstable(products, prod, N'isabout("' + @SearchKey + '" weight (1))')
您可能必须在 SSMS 中使用它来让它再次工作,但是当它在 VB 中做同样的事情时。 请注意我添加的评论:
'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
感谢您的回答,它是完整的,但我找到了一个更简单的解决方案。 这是:
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.