2014-05-05 141 views
0

enter image description here我有一些错误。我有一个来自数据库的搜索功能。当我运行该项目,并单击搜索按钮来查看列表视图,称为CON​​CAT“附近有语法错误‘’。这里的代码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, '%')" 

的搜索功能

这里全码
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

你并不需要使用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。

+0

它仍然没有工作。这是我的界面https://www.dropbox.com/s/lafqqpa52hzajz4/Listview%20Room.jpg – siyhaz

+0

你仍然得到一个例外?如果是这样,那是什么? –

+0

是的。该错误是不正确的语法附近'%' – siyhaz