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,必须调用 DB2Command 对象的 DB2®Command.ExecuteReader 方法,而不是直接使用构造函数。
您可以从使用同一 DB2连接 实例的多个 DB2DataReader 实例并发访问数据。 每个 DB2DataReader 实例都必须与其自己的 DB2命令 实例相关联。
另一个进程或线程在读取数据时对结果集所作的更改可能对 DB2DataReader的用户可见。 但是,精确的行为与时间相关。
如果应用程序需要以多个方向滚动结果集,或者插入,更新和删除行,那么可以使用 DB2ResultSet 实例。
IsClosed 和 RecordsAffected 是在 DB2DataReader 关闭后可调用的唯一属性。 在某些情况下,必须调用 Close 才能调用 RecordsAffected。
示例
[Visual Basic , C#] 以下示例创建 DB2连接 , DB2命令 和 DB2DataReader。 该示例读取数据并将它写入控制台。 最后,此示例将关闭 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)成员对于多线程操作都是安全的。 不保证任何实例成员均为线程安全。