使用文本框的 VB.net 搜索数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23041686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:21:20  来源:igfitidea点击:

VB.net Search Database using textbox

sqlvb.netvisual-studio-2010

提问by Phillip Schikora

I have a textbox in which a user can enter a hotel name and then clicks the Search button. A list of hotels with given name should be displayed upon doing so. Whenever I enter a hotel name and click the button, nothing gets displayed. Below is my code, thank you for any advice:

我有一个文本框,用户可以在其中输入酒店名称,然后单击“搜索”按钮。执行此操作时应显示具有给定名称的酒店列表。每当我输入酒店名称并单击按钮时,都不会显示任何内容。以下是我的代码,感谢您的任何建议:

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim RegDataConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & Server.MapPath("App_Data/1202389.mdb"))

    Dim SearchHotel As String = TBSearch.Text

    Dim cmd As OleDbCommand = New OleDbCommand("SELECT Hotel.Hotel_Name, Hotel.Hotel_Location FROM Hotel WHERE Hotel_Name Like '%" & SearchHotel & "%' ", RegDataConn)
    RegDataConn.Open()
    Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
    Dim myDataSet As DataSet = New DataSet()
    myDA.Fill(myDataSet, "Table")
    GridView1.DataSource = myDataSet.Tables("Table").DefaultView



End Sub

回答by Neverr Myndd

try to add plus sign(+) in you query and add single quote after percent sign(%).

尝试在查询中添加加号 (+) 并在百分号 (%) 后添加单引号。

 WHERE Hotel_Name Like '%'+" & SearchHotel & "+'%'