溫馨提示×

vb怎么查詢數(shù)據(jù)庫的數(shù)據(jù)

小億
125
2023-12-16 15:12:32
欄目: 編程語言

在VB中查詢數(shù)據(jù)庫的數(shù)據(jù),你需要使用ADO.NET對象和SQL查詢語句。下面是一個示例代碼,演示如何查詢數(shù)據(jù)庫中的數(shù)據(jù):

Imports System.Data.SqlClient

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim connectionString As String = "Data Source=YourServerName;Initial Catalog=YourDatabaseName;User ID=YourUsername;Password=YourPassword"
        Dim query As String = "SELECT * FROM YourTableName"

        Using connection As New SqlConnection(connectionString)
            connection.Open()

            Using command As New SqlCommand(query, connection)
                Dim adapter As New SqlDataAdapter(command)
                Dim table As New DataTable()
                adapter.Fill(table)

                DataGridView1.DataSource = table
            End Using
        End Using
    End Sub
End Class

在上面的代碼中,你需要將 YourServerName、YourDatabaseName、YourUsername、YourPasswordYourTableName 替換為你自己的數(shù)據(jù)庫連接信息和表名。然后,點擊按鈕 Button1 時,它會執(zhí)行查詢并將結果顯示在一個 DataGridView 控件中。你可以根據(jù)需要自定義查詢語句和顯示結果的方式。

0