使用组合框,文本框和列表视图搜索功能

时间:2014-05-05 05:03:52

标签: database vb.net

enter image description here我有一些错误。我有一个来自数据库的搜索功能。当我运行项目,并单击SEARCH按钮在列表视图中查看时,弹出消息称为"" CONCAT"附近的语法错误。这里是CONCAT的代码

Dim strSqlSearch As String = "SELECT Room_Code, Room_Type, Room_No, Room_Price, Room_Status, No_of_Occupancy" & _
                                    "FROM Room" & _
                                    "WHERE" & colName(cboSearch.SelectedIndex) & "LIKE CONCAT ('%', @valueName, '%')"

这里是SEARCH功能的完整代码

Private Sub Search()

    ListViewRoom.Items.Clear()

    Dim item As New ListViewItem
    Dim _isFound As Boolean = False

    Dim colName() As String = {"Room_Code", "Room_Type", "Room_No", "Room_Price", "Room_Status", "No_of_Occupancy"}

    Dim strSqlSearch As String = "SELECT Room_Code, Room_Type, Room_No, Room_Price, Room_Status, No_of_Occupancy" & _
                                    "FROM Room" & _
                                    "WHERE" & colName(cboSearch.SelectedIndex) & "LIKE CONCAT ('%', @valueName, '%')"

    dbSource = "Data Source=LAILATUL-PC\SERVER;Initial Catalog=HotelManagementSystem;Integrated Security=True"

    Using con As New SqlClient.SqlConnection("Data Source=LAILATUL-PC\SERVER;Initial Catalog=HotelManagementSystem;Integrated Security=True")

        Using com As New SqlClient.SqlCommand()
            With com
                .Connection = con
                .CommandType = CommandType.Text
                .CommandText = strSqlSearch
                .Parameters.AddWithValue("@valueName", txtSearch.Text)

            End With

            Try
                con.Open()
                Dim dr As SqlClient.SqlDataReader = com.ExecuteReader

                While dr.Read
                    _isFound = True

                    item = ListViewRoom.Items.Add(dr("Room_Code").ToString)
                    item.SubItems.Add(dr("Room_Type".ToString))
                    item.SubItems.Add(dr("Room_No".ToString))
                    item.SubItems.Add(dr("Room_Price".ToString))
                    item.SubItems.Add(dr("Room_Status".ToString))
                    item.SubItems.Add(dr("No_of_Occupancy".ToString))
                End While

                If Not _isFound Then
                    MsgBox("No results found.", MsgBoxStyle.OkOnly, "Information")
                End If
            Catch ex As Exception
                MsgBox(ex.Message.ToString(), MsgBoxStyle.OkOnly, "Error")
            End Try

        End Using
    End Using





End Sub

我希望你能帮助我。 TQ

1 个答案:

答案 0 :(得分:1)

您不需要使用CONCAT。只需将它从SQL中删除它就可以了。

修改

试试这个:

Dim strSqlSearch As String = "SELECT Room_Code, Room_Type, Room_No, Room_Price, Room_Status, No_of_Occupancy" & _
                             "FROM Room" & _
                             "WHERE" & colName(cboSearch.SelectedIndex) & "LIKE '%'+ @valueName +'%'"

编辑#2

Dim strSqlSearch As String = "SELECT Room_Code, Room_Type, Room_No, Room_Price, Room_Status, No_of_Occupancy" & _
                             "FROM Room" & _
                             "WHERE" & colName(cboSearch.SelectedIndex) & "LIKE '%" & txtSearch.Text & "%'"

从SqlCommand中删除参数。