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 sealed class OdbcDataReader : System.Data.Common.DbDataReader
type OdbcDataReader = class
    inherit DbDataReader
Public NotInheritable Class OdbcDataReader
Inherits DbDataReader
继承
OdbcDataReader

示例

下面的示例创建 OdbcConnectionOdbcCommandOdbcDataReaderThe 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,必须调用 OdbcCommand 对象的 ExecuteReader 方法,而不是直接使用构造函数。To create an OdbcDataReader, you must call the ExecuteReader method of the OdbcCommand object, instead of directly using a constructor.

使用 OdbcDataReader 时,关联的 OdbcConnection 忙于处理 OdbcDataReader,并且除关闭外,不能对 OdbcConnection 执行其他任何操作。While 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. 在调用 OdbcDataReaderClose 方法之前,会出现这种情况。This is the case until the Close method of the OdbcDataReader is called. 例如,在调用 Close之后,才能检索输出参数。For 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.

GetData(Int32)

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

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.

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()

返回一个表示当前对象的 string。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()

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

IEnumerable.GetEnumerator()

返回循环访问集合的枚举器。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)

获取 DbColumn 的列架构(DbDataReader 集合)。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.

适用于

另请参阅