在VB中讀取SQL數據庫數據可以使用ADO.NET技術。以下是一個簡單示例代碼:
Imports System.Data.SqlClient
Dim connStr As String = "Data Source=myServerAddress;Initial Catalog=myDatabase;User ID=myUsername;Password=myPassword;"
Dim conn As New SqlConnection(connStr)
conn.Open()
Dim query As String = "SELECT * FROM myTable"
Dim cmd As New SqlCommand(query, conn)
Dim reader As SqlDataReader = cmd.ExecuteReader()
While reader.Read()
Dim value1 As String = reader("column1").ToString()
Dim value2 As Integer = CInt(reader("column2"))
'處理讀取到的數據
End While
reader.Close()
conn.Close()
conn.Dispose()
以上示例代碼演示了如何連接到數據庫,執行查詢語句并讀取數據。在實際應用中,可以根據具體需求修改查詢語句和處理讀取數據的邏輯。