DataReaderExtensions.GetFieldValueAsync(DbDataReader, String, CancellationToken) DataReaderExtensions.GetFieldValueAsync(DbDataReader, String, CancellationToken) Method

定义

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

public static System.Threading.Tasks.Task<T> GetFieldValueAsync<T> (this System.Data.Common.DbDataReader reader, string name, System.Threading.CancellationToken cancellationToken = null);
static member GetFieldValueAsync : System.Data.Common.DbDataReader * string * System.Threading.CancellationToken -> System.Threading.Tasks.Task<'T>

类型参数

T

要返回的值的类型。The type of the value to be returned.

参数

reader
DbDataReader DbDataReader

要从中获取列值的数据读取器。The data reader to get the column value from.

name
String String

列的名称。The name of the column.

cancellationToken
CancellationToken CancellationToken

用于取消异步操作的可选令牌。An optional token to cancel the asynchronous operation.

返回

Task<T>

Result 包含指定列的值的任务。A task whose Result contains the value of the specified column.

异常

在数据检索期间删除或关闭连接。The connection was dropped or closed during data retrieval.

-or-

在数据检索期间关闭数据读取器。The data reader was closed during the data retrieval.

-or-

没有可读取的数据(例如尚未调用第一个 Read(),或调用返回了 false)。There is no data ready to be read (for example, the first Read() hasn't been called, or it returned false).

-or-

按顺序模式尝试读取上一步骤中读取的列。Tried to read a previously-read column in sequential mode.

- 或 --or-

没有正在进行的异步操作。There was an asynchronous operation in progress. 以顺序模式运行时,这适用于所有 Get_*_ 方法,读取流时也可对其进行调用。This applies to all Get_*_ methods when running in sequential mode, as they could be called while reading a stream.

指定的名称不是有效的列名。The name specified is not a valid column name.

数据库返回的值不匹配或不能转换为 TThe value returned by the database doesn't match or cannot be cast to T.

注解

仅当在顺序模式下创建读取器时, 才需要此异步方法来避免阻塞调用线程。This asynchronous method is only needed to avoid blocking the calling thread when the reader is created in sequential mode.

如果未指定顺序模式, 则每次 ReadAsync 完成时, 所有列值都将在内存中可用, 并且调用该方法的同步版本不应阻止调用线程。If sequential mode isn't specified, all column values should become available in memory each time ReadAsync completes, and calling the synchronous version of the method shouldn't block the calling thread.

此异步方法的默认实现调用其同步对应项, 并返回已完成的任务, 可能会阻止调用线程。The default implementation of this asynchronous method invokes its synchronous counterpart and returns a completed Task, potentially blocking the calling thread. 如果传递了已取消的取消标记, 则默认实现还会返回已取消的任务。The default implementation also returns a cancelled task if passed an already cancelled cancellation token.

支持异步编程的数据访问接口应使用异步 i/o 操作来重写默认的 inmplementation。Data providers that support asynchronous programming should override the default inmplementation using asynchronous I/O operations.

该方法可用于请求操作之前接受取消标记。This method accepts a cancellation token that can be used to request the operation to be cancelled early. 实现可能会忽略该请求。Implementations may ignore this request.

当返回任务还未完成时, DbDataReader 对象的其他方法和属性不应调用 。Other methods and properties of the DbDataReader object should not be invoked while the returned Task is not yet completed.

适用于