Export (0) Print
Expand All

OleDbDataReader Class

Provides a way of reading a forward-only stream of data rows from a data source. This class cannot be inherited.

For a list of all members of this type, see OleDbDataReader Members.

System.Object
   System.MarshalByRefObject
      System.Data.OleDb.OleDbDataReader

[Visual Basic]
NotInheritable Public Class OleDbDataReader
   Inherits MarshalByRefObject
   Implements IDataReader, IDisposable, IDataRecord, IEnumerable
[C#]
public sealed class OleDbDataReader : MarshalByRefObject,
   IDataReader, IDisposable, IDataRecord, IEnumerable
[C++]
public __gc __sealed class OleDbDataReader : public
   MarshalByRefObject, IDataReader, IDisposable, IDataRecord,
   IEnumerable
[JScript]
public class OleDbDataReader extends MarshalByRefObject implements
   IDataReader, IDisposable, IDataRecord, IEnumerable

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

To create an OleDbDataReader, you must call the ExecuteReader method of the OleDbCommand object, rather than directly using a constructor.

While the OleDbDataReader is in use, the associated OleDbConnection is busy serving the OleDbDataReader, and no other operations can be performed on the OleDbConnection other than closing it. This is the case until the Close method of the OleDbDataReader is called. For example, you cannot retrieve output parameters until after you call Close.

Changes made to a resultset by another process or thread while data is being read may be visible to the user of the OleDbDataReader. However, the precise behavior is timing dependent.

IsClosed and RecordsAffected are the only properties that you can call after the OleDbDataReader is closed. Though the RecordsAffected property may be accessed at any time while the OleDbDataReader exists, always call Close before returning the value of RecordsAffected to ensure an accurate return value.

Example

[Visual Basic, C#, C++] The following example creates an OleDbConnection, an OleDbCommand, and an OleDbDataReader. The example reads through the data, writing it out to the console. Finally, the example closes the OleDbDataReader, then the OleDbConnection.

[Visual Basic] 
Public Sub ReadMyData(myConnString As String)
    Dim mySelectQuery As String = "SELECT OrderID, CustomerID FROM Orders"
    Dim myConnection As New OleDbConnection(myConnString)
    Dim myCommand As New OleDbCommand(mySelectQuery, myConnection)
    myConnection.Open()
    Dim myReader As OleDbDataReader
    myReader = myCommand.ExecuteReader()
    ' Always call Read before accessing data.
    While myReader.Read()
        Console.WriteLine(myReader.GetInt32(0).ToString() + ", " _
           + myReader.GetString(1))
    End While
    ' always call Close when done reading.
    myReader.Close()
    ' Close the connection when done with it.
    myConnection.Close()
End Sub

[C#] 
public void ReadMyData(string myConnString) {
   string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders";
   OleDbConnection myConnection = new OleDbConnection(myConnString);
   OleDbCommand myCommand = new OleDbCommand(mySelectQuery,myConnection);
   myConnection.Open();
   OleDbDataReader myReader;
   myReader = myCommand.ExecuteReader();
   // Always call Read before accessing data.
   while (myReader.Read()) {
      Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
   }
   // always call Close when done reading.
   myReader.Close();
   // Close the connection when done with it.
   myConnection.Close();
}

[C++] 
public:
 void ReadMyData(String* myConnString) {
    String* mySelectQuery = S"SELECT OrderID, CustomerID FROM Orders";
    OleDbConnection* myConnection = new OleDbConnection(myConnString);
    OleDbCommand* myCommand = new OleDbCommand(mySelectQuery,myConnection);
    myConnection->Open();
    OleDbDataReader* myReader;
    myReader = myCommand->ExecuteReader();
    // Always call Read before accessing data.
    while (myReader->Read()) {
       Console::WriteLine(S"{0}, {1}", __box(myReader->GetInt32(0)), myReader->GetString(1));
    }
    // always call Close when done reading.
    myReader->Close();
    // Close the connection when done with it.
    myConnection->Close();
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Data.OleDb

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System.Data (in System.Data.dll)

See Also

OleDbDataReader Members | System.Data.OleDb Namespace

Show:
© 2014 Microsoft