DbConnection.OpenAsync 方法

定义

此方法实现 Open() 的异步版本。This method implements an asynchronous version of Open().

重载

OpenAsync()

Open()的异步版本,打开由 ConnectionString指定的设置的数据库连接。An asynchronous version of Open(), which opens a database connection with the settings specified by the ConnectionString. 此方法通过 CancellationToken.None 调用虚拟方法 OpenAsync(CancellationToken)This method invokes the virtual method OpenAsync(CancellationToken) with CancellationToken.None.

OpenAsync(CancellationToken)

这是 Open() 的异步版本。This is the asynchronous version of Open(). 提供程序应使用合适的实现进行重写。Providers should override with an appropriate implementation. 可选择性接受取消标记。The cancellation token can optionally be honored.

默认实现调用同步 Open() 调用并返回已完成任务。The default implementation invokes the synchronous Open() call and returns a completed task. 如果传递到已取消的取消标记,则默认实现将返回已取消的 cancellationToken。The default implementation will return a cancelled task if passed an already cancelled cancellationToken. Open 引发的异常将通过返回的任务异常属性传递。Exceptions thrown by Open will be communicated via the returned Task Exception property.

在返回的任务完成前,不要调用 DbConnection 对象的其他方法和属性。Do not invoke other methods and properties of the DbConnection object until the returned Task is complete.

注解

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

OpenAsync()

Open()的异步版本,打开由 ConnectionString指定的设置的数据库连接。An asynchronous version of Open(), which opens a database connection with the settings specified by the ConnectionString. 此方法通过 CancellationToken.None 调用虚拟方法 OpenAsync(CancellationToken)This method invokes the virtual method OpenAsync(CancellationToken) with CancellationToken.None.

public:
 System::Threading::Tasks::Task ^ OpenAsync();
public System.Threading.Tasks.Task OpenAsync ();
member this.OpenAsync : unit -> System.Threading.Tasks.Task
Public Function OpenAsync () As Task

返回

Task

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

注解

调用后 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.

适用于

OpenAsync(CancellationToken)

这是 Open() 的异步版本。This is the asynchronous version of Open(). 提供程序应使用合适的实现进行重写。Providers should override with an appropriate implementation. 可选择性接受取消标记。The cancellation token can optionally be honored.

默认实现调用同步 Open() 调用并返回已完成任务。The default implementation invokes the synchronous Open() call and returns a completed task. 如果传递到已取消的取消标记,则默认实现将返回已取消的 cancellationToken。The default implementation will return a cancelled task if passed an already cancelled cancellationToken. Open 引发的异常将通过返回的任务异常属性传递。Exceptions thrown by Open will be communicated via the returned Task Exception property.

在返回的任务完成前,不要调用 DbConnection 对象的其他方法和属性。Do not invoke other methods and properties of the DbConnection object until the returned Task is complete.

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

参数

cancellationToken
CancellationToken

取消指令。The cancellation instruction.

返回

Task

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

注解

调用后 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.

适用于