在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 控件中。你可以根据需要自定义查询语句和显示结果的方式。