VB.NET 中的 Like 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20941779/
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
Like statement in VB.NET
提问by Ninay11
If I enter an input into the textbox, my listview updates, but there is always no result even though my input is there.
如果我在文本框中输入一个输入,我的列表视图会更新,但即使我的输入在那里,也总是没有结果。
Private Sub searchRecord()
Dim dt As New DataTable
dt = ExecuteQuery("SELECT * FROM tblSupplier WHERE '" & cboSearch.Text
& "' LIKE '" & txtSearch.Text & "%'")
lvSupplier.Items.Clear()
If dt.Rows.Count > 0 Then
For ctr = 0 To dt.Rows.Count - 1
Dim item As New ListViewItem
item.Text = dt.Rows(ctr)("SuppID")
item.SubItems.Add(dt.Rows(ctr)("SuppName"))
item.SubItems.Add(dt.Rows(ctr)("SuppAddress"))
item.SubItems.Add(dt.Rows(ctr)("SuppConPerson"))
item.SubItems.Add(dt.Rows(ctr)("SuppConNumber"))
item.SubItems.Add(dt.Rows(ctr)("SuppEmail"))
lvSupplier.Items.Add(item)
Next
End If
End Sub
Dim dt As New DataTable
dt = ExecuteQuery("SELECT * FROM tblSupplier")
Try
If txtSearch.Text = "" Then
Call fillSupplier(dt, lvSupplier)
Else
Call searchRecord()
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
回答by chris_techno25
Your SQL statement should be like this...
你的SQL语句应该是这样的...
"SELECT * FROM tblSupplier WHERE " & cboSearch.Text & " LIKE '%" & txtSearch.Text.Replace("'","''").Trim() & "%'"
This way you should be able to search for any character or word wherever it's placed in the original word or phrase found in the database.
通过这种方式,您应该能够搜索位于数据库中原始单词或短语中的任何字符或单词。

