溫馨提示×

vb怎么實現(xiàn)查詢功能

vb
小億
137
2023-12-04 23:38:59
欄目: 編程語言

在VB中,可以使用SQL語句來實現(xiàn)查詢功能。下面是一個簡單的示例,展示如何在VB中使用SQL語句查詢數(shù)據(jù)庫:

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、YourDatabaseNameYourTableNameYourColumnName替換為你的具體數(shù)據(jù)庫和表格信息。用戶在文本框txtSearch中輸入的值將作為查詢條件進(jìn)行查詢。查詢結(jié)果將顯示在一個名為dgvResults的DataGridView控件中。

0