提供一份vb.net模糊查询代码
时间: 2023-09-02 10:07:32 浏览: 59
以下是 VB.NET 中实现模糊查询的示例代码:
```vb.net
Dim searchKey As String = "apple"
Dim connectionString As String = "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=SampleDatabase;Integrated Security=True"
Dim sqlQuery As String = "SELECT * FROM Products WHERE ProductName LIKE '%" & searchKey & "%'"
Using connection As New SqlConnection(connectionString)
Using command As New SqlCommand(sqlQuery, connection)
connection.Open()
Dim adapter As New SqlDataAdapter(command)
Dim dataSet As New DataSet
adapter.Fill(dataSet, "Products")
DataGridView1.DataSource = dataSet.Tables("Products")
End Using
End Using
```
上述代码使用了 SQL 语句中的 `LIKE` 运算符实现模糊查询,通过在查询语句中使用 `%` 通配符来匹配查询关键字的任意部分。在实际使用中,需要将 `searchKey` 替换为用户输入的查询关键字,将 `connectionString` 替换为实际的数据库连接字符串,将 `sqlQuery` 替换为实际的查询语句,以及根据需要修改数据绑定操作。