從微軟的一個SqlDataReader例如:
根據什麼來控制你要填充,以及你將如何呈現數據,有可能是一個更好的辦法來設置這些控件的數據源,但如果你想讀一個MS SQL Server數據庫和處理自己,做這樣的事情:
Option Explicit On
Option Strict On
Imports System.Data
Imports System.Data.SqlClient
Module Module1
Sub Main()
Dim str As String = "Data Source=(local);Initial Catalog=Northwind;" _
& "Integrated Security=SSPI;"
ReadOrderData(str)
End Sub
Private Sub ReadOrderData(ByVal connectionString As String)
Dim queryString As String = _
"SELECT OrderID, CustomerID FROM dbo.Orders;"
Using connection As New SqlConnection(connectionString)
Dim command As New SqlCommand(queryString, connection)
connection.Open()
Dim reader As SqlDataReader = command.ExecuteReader()
' Call Read before accessing data.
While reader.Read()
ReadSingleRow(CType(reader, IDataRecord))
End While
' Call Close when done reading.
reader.Close()
End Using
End Sub
Private Sub ReadSingleRow(ByVal record As IDataRecord)
Console.WriteLine(String.Format("{0}, {1}", record(0), record(1)))
End Sub
End Module
當然,你有哪種數據庫? –
有MySql .net連接器! http://dev.mysql.com/downloads/connector/net/ –
@MrLister這是一個Microsoft SQL Server。 – Blunderfest