DB2 Version 10.1 for Linux, UNIX, and Windows

DB2DataReader Class

Provides a way of reading a forward-only stream of data rows from a database.

Namespace:
IBM®.Data.DB2®
Assembly:
IBM.Data.DB2 (in IBM.Data.DB2.dll)

.NET Framework 2.0 3.0, 3.5, and 4.0 Inheritance hierarchy

System.Object
   System.MarshalByRefObject
      System.Data.Common.DbDataReader
         IBM.Data.DB2.DB2DataReader

.NET Framework 2.0 3.0, 3.5, and 4.0 Syntax

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

Remarks

To create a DB2DataReader, you must call the DB2Command.ExecuteReader method of the DB2Command object, rather than directly using a constructor.

You can concurrently access data from multiple DB2DataReader instances that use the same DB2Connection instance. Each DB2DataReader instance must be associated with its own DB2Command instance.

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

If your application needs to scroll through result sets in multiple directions, or insert, update, and delete rows, you can use a DB2ResultSet instance.

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

Example

[Visual Basic, C#] The following example creates a DB2Connection , a DB2Command , and a DB2DataReader. The example reads through the data, writing it out to the console. Finally, the example closes the DB2DataReader, then the DB2Connection.

[Visual Basic]
Public Sub ReadMyData(myConnString As String)
    Dim mySelectQuery As String = "SELECT SALES, SALES_PERSON FROM SALES"
    Dim myConnection As New DB2Connection(myConnString)
    Dim myCommand As New DB2Command(mySelectQuery, myConnection)
    myConnection.Open()
    Dim myReader As DB2DataReader
    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 SALES, SALES_PERSON FROM SALES";
   DB2Connection myConnection = new DB2Connection(myConnString);
   DB2Command myCommand = new DB2Command(mySelectQuery,myConnection);
   myConnection.Open();
   DB2DataReader 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();
}

Thread safety

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

Version information

Last update
This topic was last updated for: IBM DB2 Version 9.5
.NET Framework version
Supported in: 2.0, 3.0, 3.5, and 4.0
IBM Data Server Client
Supported in: 8.1.2 and up