[OleDB] Datenzugriff (SELECT)

Dim DB_OleDBConnection As System.Data.OleDb.OleDbConnection
Dim DB_OleDBCommandSelect As New System.Data.OleDb.OleDbCommand
Dim DB_OleDBReader As System.Data.OleDb.OleDbDataReader

DB_OleDBConnection = New System.Data.OleDb.OleDbConnection

With DB_OleDBConnection
    .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=MyDatabase.mdb"
    .Open()
End With

With DB_OleDBCommandSelect
    .CommandText = "SELECT ID, Name FROM Users"
    .Connection = DB_OleDBConnection
    DB_OleDBReader = .ExecuteReader()
    .Dispose()
End With

With DB_OleDBReader
    If .HasRows Then
        Do While .Read()
            System.Console.WriteLine(.Item("Name"))
        Loop
    End If
    .Close()
End With

DB_OleDBCommandSelect = Nothing

DB_OleDBConnection.Close()
DB_OleDBConnection.Dispose()
DB_OleDBConnection = Nothing

oder

Dim sConnectionString
Dim sSQL As String

sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mydatabase.mdb"
sSQL = "SELECT ID, Name FROM Users"

Dim DB_Conn As New System.Data.OleDb.OleDbConnection(sConnectionString)
Dim DB_Cmd As New System.Data.OleDb.OleDbCommand(sSQL, DB_Conn)
Dim DB_DataReader As System.Data.OleDb.OleDbDataReader

DB_Conn.Open()

DB_DataReader = DB_Cmd.ExecuteReader()

If DB_DataReader.HasRows Then
    Do While DB_DataReader.Read()
        System.Console.WriteLine(DB_DataReader.Item("Name"))
    Loop
End If

DB_DataReader.Close()
DB_Cmd.Dispose()
DB_Cmd = Nothing
DB_Conn.Close()
DB_Conn.Dispose()
DB_Conn = Nothing

Schreibe einen Kommentar