我是VB的新手。因此,当我运行程序并搜索名称时。我想在介于两者之间的任何空间中搜索。例如,我想像那样搜索" Brian Tracy"
我对可以做到这一点的字符串函数感到非常困惑。谢谢您的帮助
这是在
上工作的代码Dim search As String = txtAuthorsName.Text
'Search Sql For Authors Data
Dim conn As New
OleDb.OleDbConnection("Provider=Microsoft.ACE.OleDb.12.0;Data Source=" +
Server.MapPath("~/Access/bookstore.accdb"))
conn.Open()
Dim sql As String = "SELECT AID, authorName, authorSex FROM Authors"
Dim cmd As Object
If searchAuthor.SelectedValue.Equals("Name") Then
If search.Length > 0 Then
Dim keywords As String() = search.Split(" ")
sql = "SELECT * FROM Authors where authorName like '%" & keywords(0) & "%'"
For k As Integer = 1 To keywords.Length - 1
sql += " or authorName Like '%" & keywords(k) & "%'"
Next
End If
cmd = New OleDb.OleDbCommand(sql, conn)
cmd.Parameters.AddWithValue("@authorName", "%" + txtAuthorsName.Text + "%")
Else
sql += "WHERE authorSex LIKE @authorSex"
cmd = New OleDb.OleDbCommand(sql, conn)
cmd.Parameters.AddWithValue("@authorSex", "%" + txtAuthorSex.Text + "%")
End If
Dim dbread = cmd.ExecuteReader()
GridView2.DataSource = dbread
GridView2.DataBind()
dbread.Close()
conn.Close()
这是有关如何过滤/搜索数据的示例:
Public Sub FilterData(valueToSearch As String)
'
Dim searchQuery As String = "SELECT * From Users WHERE CONCAT(fname, lname, age) like '%" & valueToSearch & "%'"
Dim command As New SqlCommand(searchQuery, connection)
Dim adapter As New SqlDataAdapter(command)
Dim table As New DataTable()
adapter.Fill(table)
DataGridView1.DataSource = table
End Sub
Private Sub BTN_FILTER_Click(sender As Object, e As EventArgs) Handles BTN_FILTER.Click
FilterData(TextBox1.Text)
进行必要的更改,希望它有效