在VB中,可以使用SQL語句來實現查詢功能。下面是一個簡單的示例,展示如何在VB中使用SQL語句查詢數據庫:
Imports System.Data.SqlClient
Public Class Form1
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Dim connectionString As String = "Data Source=YourServerName;Initial Catalog=YourDatabaseName;Integrated Security=True"
Dim query As String = "SELECT * FROM YourTableName WHERE YourColumnName = @ValueToSearch"
Using connection As New SqlConnection(connectionString)
Using command As New SqlCommand(query, connection)
command.Parameters.AddWithValue("@ValueToSearch", txtSearch.Text)
connection.Open()
Dim adapter As New SqlDataAdapter(command)
Dim table As New DataTable()
adapter.Fill(table)
dgvResults.DataSource = table
End Using
End Using
End Sub
End Class
在上述示例中,你需要將YourServerName
、YourDatabaseName
、YourTableName
和YourColumnName
替換為你的具體數據庫和表格信息。用戶在文本框txtSearch
中輸入的值將作為查詢條件進行查詢。查詢結果將顯示在一個名為dgvResults
的DataGridView控件中。