SqlConnection.OpenAsync(CancellationToken) 方法

定义

Open() 的异步版本,打开 ConnectionString 指定的属性设置的数据库连接。An asynchronous version of Open(), which opens a database connection with the property settings specified by the ConnectionString. 取消标记可用于请求在连接超时超过前放弃操作。The cancellation token can be used to request that the operation be abandoned before the connection timeout elapses. 异常将通过返回的任务传播。Exceptions will be propagated via the returned Task. 如果在未成功连接的情况下连接超时时间已过,则返回的任务将被标记为“附带异常出错”。If the connection timeout time elapses without successfully connecting, the returned Task will be marked as faulted with an Exception. 实现返回一个不带有用于共用和非共用连接的锁定调用线程的“任务”。The implementation returns a Task without blocking the calling thread for both pooled and non-pooled connections.

public:
 override System::Threading::Tasks::Task ^ OpenAsync(System::Threading::CancellationToken cancellationToken);
public override System.Threading.Tasks.Task OpenAsync (System.Threading.CancellationToken cancellationToken);
override this.OpenAsync : System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Overrides Function OpenAsync (cancellationToken As CancellationToken) As Task

参数

cancellationToken
CancellationToken

取消指令。The cancellation instruction.

返回

Task

表示异步操作的任务。A task representing the asynchronous operation.

例外

在完成任务前,为同一个实例多次调用 OpenAsync(CancellationToken)Calling OpenAsync(CancellationToken) more than once for the same instance before task completion.

在连接字符串中指定 Context Connection=trueContext Connection=true is specified in the connection string.

在经过连接超时时间之前从连接池中的连接不可用。A connection was not available from the connection pool before the connection time out elapsed.

打开连接时出现的任何由 SQL Server 返回的错误。Any error returned by SQL Server that occurred while opening the connection.

注解

调用后 OpenAsyncState 必须返回 Connecting 直到返回 Task 完成。After calling OpenAsync, State must return Connecting until the returned Task is completed. 然后,如果连接成功,则 State 必须返回 OpenThen, if the connection was successful, State must return Open. 如果连接失败,则 State 必须返回 ClosedIf the connection fails, State must return Closed.

对的调用 Close 将尝试取消或关闭相应的 OpenAsync 调用。A call to Close will attempt to cancel or close the corresponding OpenAsync call.

有关 SQL Server 的 .NET Framework 数据提供程序中的异步编程的详细信息,请参阅 异步编程For more information about asynchronous programming in the .NET Framework Data Provider for SQL Server, see Asynchronous Programming.

适用于