[英]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.