简体   繁体   English

SQL 查询包含 / isabout 在 SQL Server Management Studio 中完美运行,但它不在代码后面

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

相关问题 为什么此SQL代码在Microsoft Access中不起作用,但在SQL Server Management Studio中起作用? - Why does this SQL code not work in Microsoft Access but works in SQL Server Management Studio? 为什么在SQL Server 2005 Management Studio中可用的更新查询不能在Java中用作预准备查询? - Why does my update query that works in SQL Server 2005 Management Studio not work as a Prepared Query in Java? SQL Server,ISABOUT,加权术语 - SQL Server, ISABOUT, weighted terms 在SQL Server Management Studio中,此类查询运行速度更快吗? - In SQL Server Management Studio, does this type of query run faster? 查询(SQL Server 2008 Express)在SQL Server Management Studio中工作,但在Delphi中不使用ADODB - Query (SQL Server 2008 Express) works in SQL Server Management Studio but not in Delphi using ADODB SQL 查询字符串在 SQL Server Management Studio 中有效,但在使用 SQLCommand.ExecuteReader 的 VB.net 中无效 - SQL Query String Works in SQL Server Management Studio, But Not in VB.net with SQLCommand.ExecuteReader vb.net SQL查询可在SQL Server Management Studio中使用,但不能在程序中使用 - vb.net SQL query works in SQL Server Management Studio but not in program SQL查询可在SQL Server Management Studio中使用,但不适用于ASP.Net应用程序 - SQL query works in SQL Server Management Studio but not from ASP.Net application 查询在SQL Server Management Studio中不起作用 - Query is not working in SQL Server Management Studio 查询在SQL Server Management Studio中运行,但不在sqlcmd中运行 - Query runs in SQL Server Management Studio but not in sqlcmd
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM