关键字 'Like' 附近的语法不正确。 vb净

Incorrect syntax near the keyword 'Like'. vb net

我选择了下拉列表和文本框 从 sqlserver 到 asp vb.net 中的另一种形式 但给我错误 语法不正确 脚本是

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Len(Session("LibuserID")) = 0 Then
            Response.Redirect("./index.aspx")
        End If
        Dim DBConn As SqlConnection
        Dim DBCommand As SqlDataAdapter 
        Dim DSPageData As New DataSet
        DBConn = New SqlConnection("Data Source=localhost;" & _
       "initial catalog=test;Integrated Security=True;")
        If Request.QueryString("Type") = "Search" Then
            lblMessage.Text = "Resultati Poiska:"
            DBCommand = New SqlDataAdapter _
                ("Select LibBookID,BookTitle,Author,Status " _
                 & "from LibBooks where " _
                 & Request.QueryString("ddlSearchField") & "Like '%" _
                 & Replace(Request.QueryString("txtSearchText"), "'", "''") _
                 & "&' order by BookTitle", DBConn)
        ElseIf Request.QueryString("Type") = "Browse" Then
            lblMessage.Text = "kniqi otnosyasiesya k etoy kategorii:"
            DBCommand = New SqlDataAdapter _
                ("select LibBookID,BookTitle,Author,Status " _
                 & "from LibBooks where " _
                 & "LibBookCategoryID = " _
                 & Request.QueryString("LibBookCategoryID") _
                 & "Order By BookTitle", DBConn)
        Else
            Response.Redirect("./menu.aspx")
        End If
        DBCommand.Fill(DSPageData, _
                       "Books")
        dbBooks.DataSource = _
            DSPageData.Tables("Books").DefaultView
        dbBooks.DataBind()

End Sub

错误是

关键字 'Like' 附近的语法不正确。

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'Like'.

Source Error: 


Line 33:             Response.Redirect("./menu.aspx")
Line 34:         End If
Line 35:         DBCommand.Fill(DSPageData, _
Line 36:                        "Books")
Line 37:         dbBooks.DataSource = _

正如有人所说,您应该使用参数化查询而不是这个。 可能你的问题是 Request.QueryString("ddlSearchField") 为 null 或空,所以如果你想将查询更改为参数化,你必须重写所有,如果你只是想让它工作,你必须检查值是否为 null 或空。

在你的 LIKE 子句前加上 space。

& Request.QueryString("ddlSearchField") & " Like '%" _