Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

OdbcDataReader Class

Note: This namespace, class, or member is supported only in version 1.1 of the .NET Framework.

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 OdbcDataReader Members.

System.Object
   System.MarshalByRefObject
      System.Data.Odbc.OdbcDataReader

[Visual Basic]
NotInheritable Public Class OdbcDataReader
   Inherits MarshalByRefObject
   Implements IDataReader, IDisposable, IDataRecord, IEnumerable
[C#]
public sealed class OdbcDataReader : MarshalByRefObject,
   IDataReader, IDisposable, IDataRecord, IEnumerable
[C++]
public __gc __sealed class OdbcDataReader : public
   MarshalByRefObject, IDataReader, IDisposable, IDataRecord,
   IEnumerable
[JScript]
public class OdbcDataReader 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 OdbcDataReader, you must call the ExecuteReader method of the OdbcCommand object, rather than directly using a constructor.

While the OdbcDataReader is in use, the associated OdbcConnection is busy serving the OdbcDataReader, and no other operations can be performed on the OdbcConnection other than closing it. This is the case until the Close method of the OdbcDataReader 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 OdbcDataReader. However, the precise behavior is both driver and timing dependent.

IsClosed and RecordsAffected are the only properties that you can call after the OdbcDataReader is closed. In some cases, you must call Close before you can call RecordsAffected.

Example

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

[Visual Basic] 
Public Sub ReadMyData(myConnString As String)
    Dim mySelectQuery As String = "SELECT OrderID, CustomerID FROM Orders"
    Dim myConnection As New OdbcConnection(myConnString)
    Dim myCommand As New OdbcCommand(mySelectQuery, myConnection)
    myConnection.Open()
    Dim myReader As OdbcDataReader
    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";
   OdbcConnection myConnection = new OdbcConnection(myConnString);
   OdbcCommand myCommand = new OdbcCommand(mySelectQuery,myConnection);
   myConnection.Open();
   OdbcDataReader 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";
        OdbcConnection* myConnection = new OdbcConnection(myConnString);
        OdbcCommand* myCommand = new OdbcCommand(mySelectQuery, myConnection);
        myConnection->Open();
        OdbcDataReader* 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.Odbc

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

OdbcDataReader Members | System.Data.Odbc Namespace

Show:
© 2014 Microsoft