DB2 10.5 for Linux, UNIX, and Windows

DB2DataReader 类

提供了一种从数据库读取仅正向数据行流的方法。

名称空间:
IBM.Data.DB2
组合件:
IBM.Data.DB2(在 IBM.Data.DB2.dll 中)

.NET Framework 2.0、3.0、3.5 和 4.0 继承层次结构

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

.NET Framework 2.0、3.0、3.5 和 4.0 语法

[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

注释

要创建 DB2DataReader,必须调用 DB2®Command 对象的 DB2Command.ExecuteReader 方法,而不是直接使用构造函数。

可以同时访问使用同一个 DB2Connection 实例的多个 DB2DataReader 实例中的数据。每个 DB2DataReader 实例都必须与它自己的 DB2Command 实例相关联。

在读取数据期间由另一进程或线程对结果集所作的更改对于 DB2DataReader 的用户可能是可视的。但是,精确的行为与时间相关。

如果应用程序需要从多个方向浏览结果集,或者插入、更新和删除行,那么可以使用 DB2ResultSet 实例。

在关闭 DB2DataReader 之后,IsClosedRecordsAffected 是唯一可以调用的属性。在某些情况下,必须调用 Close 才能调用 RecordsAffected

示例

[Visual Basic, C#] 以下示例将创建 DB2ConnectionDB2CommandDB2DataReader。该示例读取数据并将它写入控制台。最后,该示例关闭 DB2DataReader,然后关闭 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();
}

线程安全

此类型的任何公用静态(Visual Basic 中的 Shared)成员对于多线程操作都是安全的。 不保证任何实例成员均为线程安全。