在VB中查詢數據庫的數據,你需要使用ADO.NET對象和SQL查詢語句。下面是一個示例代碼,演示如何查詢數據庫中的數據:
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
、YourPassword
和 YourTableName
替換為你自己的數據庫連接信息和表名。然后,點擊按鈕 Button1
時,它會執行查詢并將結果顯示在一個 DataGridView 控件中。你可以根據需要自定義查詢語句和顯示結果的方式。