How to ADO.NET OleDbDataReader

The OleDbDataReader Object in ADO.NET offers a connection-oriented data access to OLEDB data sources. When the ExecuteReader() method is invoked in the OleDbCommand Object, it sends the SQL statements to the OleDbConnection Object and generates an OleDbDataReader Object based on the provided SQL statement. As a result of executing the ExecuteReader() method in the OleDbCommand Object, an instance of the OleDbDataReader Object, specifically from the OleDb namespace, is instantiated.

Dim oledbReader As OleDbDataReader = oledbCmd.ExecuteReader()

In order to retrieve data from an OleDbDataReader, it is essential for the reader to be open and positioned prior to the first record. This means that before accessing any data values, it is necessary to call the Read() method on the OleDbDataReader Object to advance to the initial row.

Read() method

The Read() method in the OleDbDataReader Object is utilized to traverse the rows of data within the OleDbDataReader. Each invocation of the Read() method moves the reader forward to the next valid row, if any rows exist. In this manner, the Read() method enables sequential traversal through the result set, continually progressing to a new valid row until there are no more rows to retrieve.

OleDbDataReader.Read()

Consequently, the Read() method is typically employed within a loop construct to iterate through the OleDbDataReader and access the data from each row. Each iteration of the Read() method advances the OleDbDataReader Object to the subsequent row, facilitating the retrieval of data values using the appropriate GetXXX methods, where XXX represents the data type of the column.

It is important to emphasize that the Read() method of the OleDbDataReader Object exclusively moves forward to the next valid row in the result set. It does not allow for backward movement or repositioning to a previous row. Thus, the Read() method is designed to be utilized in a forward-only manner, reading rows sequentially.

Full Source VB.NET
Imports System.Data.OleDb Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim connetionString As String Dim oledbCnn As OleDbConnection Dim oledbCmd As OleDbCommand Dim sql As String connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;" sql = "Your SQL Statement Here like Select * from product" oledbCnn = New OleDbConnection(connetionString) Try oledbCnn.Open() oledbCmd = New OleDbCommand(sql, oledbCnn) Dim oledbReader As OleDbDataReader = oledbCmd.ExecuteReader() While oledbReader.Read MsgBox(oledbReader.Item(0) & " - " & oledbReader.Item(1) & " - " & oledbReader.Item(2)) End While oledbReader.Close() oledbCmd.Dispose() oledbCnn.Close() Catch ex As Exception MsgBox("Can not open connection ! ") End Try End Sub End Class

Conclusion

The ExecuteReader() method in the OleDbCommand Object serves as the mechanism for generating an OleDbDataReader Object, enabling connection-oriented data access to OLEDB data sources. By utilizing the Read() method, the OleDbDataReader Object can be navigated row by row, progressively advancing to new valid rows, if available, in order to read and retrieve data from the OleDbDataReader.