OdbcDataReader 类

定义

提供从数据源读取数据行的只进流的方法。Provides a way of reading a forward-only stream of data rows from a data source. 此类不能被继承。This class cannot be inherited.

public ref class OdbcDataReader sealed : System::Data::Common::DbDataReader
public ref class OdbcDataReader sealed : MarshalByRefObject, IDisposable, System::Collections::IEnumerable, System::Data::IDataReader
public sealed class OdbcDataReader : System.Data.Common.DbDataReader
public sealed class OdbcDataReader : MarshalByRefObject, IDisposable, System.Collections.IEnumerable, System.Data.IDataReader
type OdbcDataReader = class
    inherit DbDataReader
type OdbcDataReader = class
    inherit MarshalByRefObject
    interface IDataReader
    interface IDataRecord
    interface IDisposable
    interface IEnumerable
Public NotInheritable Class OdbcDataReader
Inherits DbDataReader
Public NotInheritable Class OdbcDataReader
Inherits MarshalByRefObject
Implements IDataReader, IDisposable, IEnumerable
继承
OdbcDataReader
继承
继承
OdbcDataReader
实现

示例

下面的示例创建一个 OdbcConnection 、一个 OdbcCommand 和一个 OdbcDataReaderThe following example creates an OdbcConnection, an OdbcCommand, and an OdbcDataReader. 该示例将读取数据,并将其写出到控制台。The example reads through the data, writing it out to the console. 最后,该示例关闭 OdbcDataReader ,然后关闭 OdbcConnectionFinally, the example closes the OdbcDataReader, and then the OdbcConnection.

public static void ReadData(string connectionString)
{
    string queryString = "SELECT DISTINCT CustomerID FROM Orders";

    using (OdbcConnection connection = new OdbcConnection(connectionString))
    {
        OdbcCommand command = new OdbcCommand(queryString, connection);

        connection.Open();

        // Execute the DataReader and access the data.
        OdbcDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            Console.WriteLine("CustomerID={0}", reader[0]);
        }

        // Call Close when done reading.
        reader.Close();
    }
}
Public Sub ReadData(ByVal connectionString As String)
    Dim queryString As String = "SELECT DISTINCT CustomerID FROM Orders"

    Using connection As New OdbcConnection(connectionString)
        Dim command As New OdbcCommand(queryString, connection)

        connection.Open()

        Dim reader As OdbcDataReader = command.ExecuteReader()

        While reader.Read()
            Console.WriteLine("CustomerID={0}", reader(0).ToString)
        End While

        ' Call Close when done reading.
        reader.Close()
    End Using
End Sub

注解

若要创建 OdbcDataReader ,必须调用对象的 ExecuteReader 方法 OdbcCommand ,而不是直接使用构造函数。To create an OdbcDataReader, you must call the ExecuteReader method of the OdbcCommand object, instead of directly using a constructor.

如果正在 OdbcDataReader 使用,则关联的 OdbcConnection 忙于提供服务 OdbcDataReader ,并且除关闭外,不能在其他操作上执行任何其他操作 OdbcConnectionWhile the OdbcDataReader is being used, the associated OdbcConnection is busy serving the OdbcDataReader, and no other operations can be performed on the OdbcConnection other than closing it. 在调用的方法之前,会出现这种情况 Close OdbcDataReaderThis is the case until the Close method of the OdbcDataReader is called. 例如,在调用之后,才能检索输出参数 CloseFor example, you cannot retrieve output parameters until after you call Close.

读取数据时,另一进程或线程对结果集所做的更改可能对 OdbcDataReader 的用户可见。Changes made to a result set 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.

IsClosedRecordsAffected 是在关闭 OdbcDataReader 后可以调用的唯一属性。IsClosed and RecordsAffected are the only properties that you can call after the OdbcDataReader is closed. 有时,您必须先调用, Close 然后才能调用 RecordsAffectedSometimes, you must call Close before you can call RecordsAffected.

属性

Depth

获取一个值,用于指示当前行的嵌套深度。Gets a value that indicates the depth of nesting for the current row.

FieldCount

获取当前行中的列数。Gets the number of columns in the current row.

HasRows

获取一个值,该值指示 OdbcDataReader 是否包含一行还是多行。Gets a value that indicates whether the OdbcDataReader contains one or more rows.

IsClosed

指示 OdbcDataReader 是否关闭。Indicates whether the OdbcDataReader is closed.

Item[Int32]

在给定列序号的情况下,获取指定列的以本机格式表示的值。Gets the value of the specified column in its native format given the column ordinal.

Item[String]

在给定列名称的情况下,获取指定列的以本机格式表示的值。Gets the value of the specified column in its native format given the column name.

RecordsAffected

通过执行 SQL 语句获取更改、插入或删除的行数。Gets the number of rows changed, inserted, or deleted by execution of the SQL statement.

VisibleFieldCount

获取 DbDataReader 中未隐藏的字段的数目。Gets the number of fields in the DbDataReader that are not hidden.

(继承自 DbDataReader)

方法

Close()

关闭 OdbcDataReader 对象。Closes the OdbcDataReader object.

CloseAsync()

异步关闭 DbDataReader 对象。Asynchronously closes the DbDataReader object.

(继承自 DbDataReader)
CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(继承自 MarshalByRefObject)
Dispose()

释放 DbDataReader 类的当前实例所使用的所有资源。Releases all resources used by the current instance of the DbDataReader class.

(继承自 DbDataReader)
Dispose(Boolean)

释放由 DbDataReader 占用的非托管资源,还可以另外再释放托管资源。Releases the unmanaged resources used by the DbDataReader and optionally releases the managed resources.

(继承自 DbDataReader)
DisposeAsync()

异步释放 DbDataReader 类的当前实例所使用的所有资源。Asynchronously releases all resources used by the current instance of the DbDataReader class.

(继承自 DbDataReader)
Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
Finalize()

允许对象在被“垃圾回收”之前尝试释放资源并执行其他清理操作。Allows the object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.

GetBoolean(Int32)

获取指定列的布尔值形式的值。Gets the value of the specified column as a Boolean.

GetByte(Int32)

以字节的形式获取指定列的值。Gets the value of the specified column as a byte.

GetBytes(Int32, Int64, Byte[], Int32, Int32)

从指定的列偏移量将字节流作为数组从特定的缓冲区偏移量开始读入缓冲区。Reads a stream of bytes from the specified column offset into the buffer as an array, starting at the particular buffer offset.

GetChar(Int32)

获取指定列的字符形式的值。Gets the value of the specified column as a character.

GetChars(Int32, Int64, Char[], Int32, Int32)

从指定的列偏移量将字符流作为数组从特定的缓冲区偏移量开始读入缓冲区。Reads a stream of characters from the specified column offset into the buffer as an array, starting at the particular buffer offset.

GetColumnSchemaAsync(CancellationToken)

这是 GetColumnSchema(DbDataReader) 的异步版本。This is the asynchronous version of GetColumnSchema(DbDataReader). 提供程序应使用合适的实现进行重写。Providers should override with an appropriate implementation. 可以选择接受 cancellationTokenThe cancellationToken can optionally be honored. 默认实现调用同步 GetColumnSchema(DbDataReader) 调用并返回已完成任务。The default implementation invokes the synchronous GetColumnSchema(DbDataReader) call and returns a completed task. 如果传递到已取消 cancellationToken,则默认实现将返回已取消的任务。The default implementation will return a cancelled task if passed an already cancelled cancellationToken. GetColumnSchema(DbDataReader) 引发的异常将通过任务异常属性传递。Exceptions thrown by GetColumnSchema(DbDataReader) will be communicated via the returned Task Exception property.

(继承自 DbDataReader)
GetData(Int32)

返回指定列序号的 IDataReaderReturns an IDataReader for the specified column ordinal.

GetData(Int32)

返回所请求列的嵌套数据读取器。Returns a nested data reader for the requested column.

(继承自 DbDataReader)
GetDataTypeName(Int32)

获取源数据类型的名称。Gets the name of the source data type.

GetDate(Int32)

DateTime 对象的形式获取指定列的值。Gets the value of the specified column as a DateTime object.

GetDateTime(Int32)

DateTime 对象的形式获取指定列的值。Gets the value of the specified column as a DateTime object.

GetDbDataReader(Int32)

返回被请求的列序号的 DbDataReader 对象,可以使用提供程序特定的实现对该对象进行重写。Returns a DbDataReader object for the requested column ordinal that can be overridden with a provider-specific implementation.

(继承自 DbDataReader)
GetDecimal(Int32)

Decimal 对象的形式获取指定列的值。Gets the value of the specified column as a Decimal object.

GetDouble(Int32)

获取指定列的双精度浮点数形式的值。Gets the value of the specified column as a double-precision floating-point number.

GetEnumerator()

返回一个可用于循环访问数据读取器中的行的 IEnumeratorReturns an IEnumerator that can be used to iterate through the rows in the data reader.

GetFieldType(Int32)

获取作为对象的数据类型的 TypeGets the Type that is the data type of the object.

GetFieldValue<T>(Int32)

获取指定列的值作为请求的类型。Gets the value of the specified column as the requested type.

(继承自 DbDataReader)
GetFieldValueAsync<T>(Int32)

异步获取指定列的值作为请求的类型。Asynchronously gets the value of the specified column as the requested type.

(继承自 DbDataReader)
GetFieldValueAsync<T>(Int32, CancellationToken)

异步获取指定列的值作为请求的类型。Asynchronously gets the value of the specified column as the requested type.

(继承自 DbDataReader)
GetFloat(Int32)

获取指定列的单精度浮点数形式的值。Gets the value of the specified column as a single-precision floating-point number.

GetGuid(Int32)

作为全局唯一标识符 (GUID) 获取指定列的值。Gets the value of the specified column as a globally unique identifier (GUID).

GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetInt16(Int32)

获取指定列的 16 位有符号整数形式的值。Gets the value of the specified column as a 16-bit signed integer.

GetInt32(Int32)

获取指定列的 32 位带符号整数形式的值。Gets the value of the specified column as a 32-bit signed integer.

GetInt64(Int32)

以 64 位有符号整数的形式获取指定列的值。Gets the value of the specified column as a 64-bit signed integer.

GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(继承自 MarshalByRefObject)
GetName(Int32)

获取指定列的名称。Gets the name of the specified column.

GetOrdinal(String)

在给定列名时获取相应的列序号。Gets the column ordinal, given the name of the column.

GetProviderSpecificFieldType(Int32)

获取指定列的特定于提供程序的类型。Gets the provider-specific type of the specified column.

(继承自 DbDataReader)
GetProviderSpecificValue(Int32)

以特定于提供程序的类型实例的形式获取指定列的值。Gets the value of the specified column as an instance of a provider-specific type.

(继承自 DbDataReader)
GetProviderSpecificValues(Object[])

在当前行的集合中获取提供程序特定的所有属性列。Gets all provider-specific attribute columns in the collection for the current row.

(继承自 DbDataReader)
GetSchemaTable()

返回一个 DataTable,它描述 OdbcDataReader 的列元数据。Returns a DataTable that describes the column metadata of the OdbcDataReader.

GetSchemaTableAsync(CancellationToken)

这是 GetSchemaTable() 的异步版本。This is the asynchronous version of GetSchemaTable(). 提供程序应使用合适的实现进行重写。Providers should override with an appropriate implementation. 可以选择接受 cancellationTokenThe cancellationToken can optionally be honored. 默认实现调用同步 GetSchemaTable() 调用并返回已完成任务。The default implementation invokes the synchronous GetSchemaTable() call and returns a completed task. 如果传递到已取消 cancellationToken,则默认实现将返回已取消的任务。The default implementation will return a cancelled task if passed an already cancelled cancellationToken. GetSchemaTable() 引发的异常将通过任务异常属性传递。Exceptions thrown by GetSchemaTable() will be communicated via the returned Task Exception property.

(继承自 DbDataReader)
GetStream(Int32)

获取从指定列检索数据的流。Gets a stream to retrieve data from the specified column.

(继承自 DbDataReader)
GetString(Int32)

获取指定列的 String 形式的值。Gets the value of the specified column as a String.

GetTextReader(Int32)

获取文本读取器以从列中检索数据。Gets a text reader to retrieve data from the column.

(继承自 DbDataReader)
GetTime(Int32)

TimeSpan 对象的形式获取指定列的值。Gets the value of the specified column as a TimeSpan object.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
GetValue(Int32)

获取以本机格式表示的指定序号处的列的值。Gets the value of the column at the specified ordinal in its native format.

GetValues(Object[])

使用当前行的列值来填充对象数组。Populates an array of objects with the column values of the current row.

InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance.

(继承自 MarshalByRefObject)
IsDBNull(Int32)

获取一个值,该值指示列中是否包含不存在或丢失的值。Gets a value that indicates whether the column contains nonexistent or missing values.

IsDBNullAsync(Int32)

异步获取一个值,该值指示列中是否包含不存在的或缺少的值。Asynchronously gets a value that indicates whether the column contains non-existent or missing values.

(继承自 DbDataReader)
IsDBNullAsync(Int32, CancellationToken)

异步获取一个值,该值指示列中是否包含不存在的或缺少的值。Asynchronously gets a value that indicates whether the column contains non-existent or missing values.

(继承自 DbDataReader)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

(继承自 MarshalByRefObject)
NextResult()

读取批处理 SQL 语句的结果时,使 OdbcDataReader 前进到下一个结果。Advances the OdbcDataReader to the next result when reading the results of batch SQL statements.

NextResultAsync()

在读取一批语句的结果时,使读取器异步前进到下一个结果。Asynchronously advances the reader to the next result when reading the results of a batch of statements.

(继承自 DbDataReader)
NextResultAsync(CancellationToken)

在读取一批语句的结果时,使读取器异步前进到下一个结果。Asynchronously advances the reader to the next result when reading the results of a batch of statements.

(继承自 DbDataReader)
Read()

OdbcDataReader 前进到下一条记录。Advances the OdbcDataReader to the next record.

ReadAsync()

使读取器异步前进到结果集中的下一条记录。Asynchronously advances the reader to the next record in a result set.

(继承自 DbDataReader)
ReadAsync(CancellationToken)

使读取器异步前进到结果集中的下一条记录。Asynchronously advances the reader to the next record in a result set.

(继承自 DbDataReader)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

显式接口实现

IDataReader.Close()

有关此成员的说明,请参见 Close()For a description of this member, see Close().

(继承自 DbDataReader)
IDataReader.GetSchemaTable()

有关此成员的说明,请参见 GetSchemaTable()For a description of this member, see GetSchemaTable().

(继承自 DbDataReader)
IDataRecord.GetData(Int32)

有关此成员的说明,请参见 GetData(Int32)For a description of this member, see GetData(Int32).

(继承自 DbDataReader)
IDisposable.Dispose()

此 API 支持产品基础结构,不能在代码中直接使用。

释放 OdbcDataReader 类的当前实例所使用的所有资源。Releases all resources used by the current instance of the OdbcDataReader class.

IEnumerable.GetEnumerator()

此 API 支持产品基础结构,不能在代码中直接使用。

返回循环访问集合的枚举数。Returns an enumerator that iterates through a collection.

扩展方法

CanGetColumnSchema(DbDataReader)

获取一个值,指示 DbDataReader 是否可以获取列架构。Gets a value that indicates whether a DbDataReader can get a column schema.

GetColumnSchema(DbDataReader)

获取 DbDataReader 的列架构(DbColumn 集合)。Gets the column schema (DbColumn collection) for a DbDataReader.

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

适用于

另请参阅