文档主页
MySQL Connector/NET 开发人员指南
相关文档 下载本手册
PDF (US Ltr) - 1.3Mb
PDF (A4) - 1.3Mb


5.14 异步方法

基于任务的异步模式 (TAP) 是 .NET Framework 中异步的模式。它基于 System.Threading.Tasks 命名空间中的 TaskTask<TResult> 类型,用于表示任意异步操作。

Async-Await 是用于处理 TAP 的新关键字。 Async 修饰符 用于指定方法、lambda 表达式或匿名方法是异步的。 Await 运算符应用于异步方法中的任务,以挂起方法的执行,直到等待的任务完成。

需求

  • Async-Await 支持需要 .NET Framework 4.5 或更高版本

  • TAP 支持需要 .NET Framework 4.0 或更高版本

  • MySQL Connector/NET 6.9 或更高版本

方法

以下方法可以使用 TAP 或 Async-Await。

  • 命名空间 MySql.Data.Entity

    • EFMySqlCommand

      • Task PrepareAsync()

      • Task PrepareAsync(CancellationToken)

  • 命名空间 MySql.Data

    • MySqlBulkLoader

      • Task<int> LoadAsync()

      • Task<int> LoadAsync(CancellationToken

    • MySqlConnection

      • Task<MySqlTransaction> BeginTransactionAsync()

      • Task<MySqlTransaction> BeginTransactionAsync (CancellationToken)

      • Task<MySqlTransaction> BeginTransactionAsync(IsolationLevel)

      • Task<MySqlTransaction> BeginTransactionAsync (IsolationLevel , CancellationToken)

      • Task ChangeDatabaseAsync(string)

      • Task ChangeDatabaseAsync(string, CancellationToken)

      • Task CloseAsync()

      • Task CloseAsync(CancellationToken)

      • Task ClearPoolAsync(MySqlConnection)

      • Task ClearPoolAsync(MySqlConnection, CancellationToken)

      • Task ClearAllPoolsAsync()

      • Task ClearAllPoolsAsync(CancellationToken)

      • Task<MySqlSchemaCollection> GetSchemaCollection(string, string[])

      • Task<MySqlSchemaCollection> GetSchemaCollection(string, string[], CancellationToken)

    • MySqlDataAdapter

      • Task<int> FillAsync(DataSet)

      • Task<int> FillAsync(DataSet, CancellationToken)

      • Task<int> FillAsync(DataTable)

      • Task<int> FillAsync(DataTable, CancellationToken)

      • Task<int> FillAsync(DataSet, string)

      • Task<int> FillAsync(DataSet, string, CancellationToken)

      • Task<int> FillAsync(DataTable, IDataReader)

      • Task<int> FillAsync(DataTable, IDataReader, CancellationToken)

      • Task<int> FillAsync(DataTable, IDbCommand, CommandBehavior)

      • Task<int> FillAsync(DataTable, IDbCommand, CommandBehavior, CancellationToken)

      • Task<int> FillAsync(int, int, params DataTable[])

      • Task<int> FillAsync(int, int, params DataTable[], CancellationToken)

      • Task<int> FillAsync(DataSet, int, int, string)

      • Task<int> FillAsync(DataSet, int, int, string, CancellationToken)

      • Task<int> FillAsync(DataSet, string, IDataReader, int, int)

      • Task<int> FillAsync(DataSet, string, IDataReader, int, int, CancellationToken)

      • Task<int> FillAsync(DataTable[], int, int, IDbCommand, CommandBehavior)

      • Task<int> FillAsync(DataTable[], int, int, IDbCommand, CommandBehavior, CancellationToken)

      • Task<int> FillAsync(DataSet, int, int, string, IDbCommand, CommandBehavior)

      • Task<int> FillAsync(DataSet, int, int, string, IDbCommand, CommandBehavior, CancellationToken)

      • Task<DataTable[]> FillSchemaAsync(DataSet, SchemaType)

      • Task<DataTable[]> FillSchemaAsync(DataSet, SchemaType, CancellationToken)

      • Task<DataTable[]> FillSchemaAsync(DataSet, SchemaType, string)

      • Task<DataTable[]> FillSchemaAsync(DataSet, SchemaType, string, CancellationToken)

      • Task<DataTable[]> FillSchemaAsync(DataSet, SchemaType, string, IDataReader)

      • Task<DataTable[]> FillSchemaAsync(DataSet, SchemaType, string, IDataReader, CancellationToken)

      • Task<DataTable[]> FillSchemaAsync(DataSet, SchemaType, IDbCommand, string, CommandBehavior)

      • Task<DataTable[]> FillSchemaAsync(DataSet, SchemaType, IDbCommand, string, CommandBehavior, CancellationToken)

      • Task<DataTable> FillSchemaAsync(DataTable, SchemaType)

      • Task<DataTable> FillSchemaAsync(DataTable, SchemaType, CancellationToken)

      • Task<DataTable> FillSchemaAsync(DataTable, SchemaType, IDataReader)

      • Task<DataTable> FillSchemaAsync(DataTable, SchemaType, IDataReader, CancellationToken)

      • Task<DataTable> FillSchemaAsync(DataTable, SchemaType, IDbCommand, CommandBehavior)

      • Task<DataTable> FillSchemaAsync(DataTable, SchemaType, IDbCommand, CommandBehavior, CancellationToken)

      • Task<int> UpdateAsync(DataRow[])

      • Task<int> UpdateAsync(DataRow[], CancellationToken)

      • Task<int> UpdateAsync(DataSet)

      • Task<int> UpdateAsync(DataSet, CancellationToken)

      • Task<int> UpdateAsync(DataTable)

      • Task<int> UpdateAsync(DataTable, CancellationToken)

      • Task<int> UpdateAsync(DataRow[], DataTableMapping, CancellationToken)

      • Task<int> UpdateAsync(DataSet, string)

      • Task<int> UpdateAsync(DataSet, string, CancellationToken)

    • MySqlHelper

      • Task<DataRow> ExecuteDataRowAsync(string, string, params MySqlParameter[])

      • Task<DataRow> ExecuteDataRowAsync(string, string, CancellationToken, params MySqlParameter[])

      • Task<int> ExecuteNonQueryAsync(MySqlConnection, string, params MySqlParameter[])

      • Task<int> ExecuteNonQueryAsync(MySqlConnection, string, CancellationToken, params MySqlParameter[])

      • Task<int> ExecuteNonQueryAsync(string, string, params MySqlParameter[])

      • Task<int> ExecuteNonQueryAsync(string, string, CancellationToken, params MySqlParameter[])

      • Task<DataSet> ExecuteDatasetAsync(string, string)

      • Task<DataSet> ExecuteDatasetAsync(string, string, CancellationToken)

      • Task<DataSet> ExecuteDatasetAsync(string, string, CancellationToken, params MySqlParameter[])

      • Task<DataSet> ExecuteDatasetAsync(MySqlConnection, string)

      • Task<DataSet> ExecuteDatasetAsync(MySqlConnection, string, CancellationToken)

      • Task<DataSet> ExecuteDatasetAsync(MySqlConnection, string, params MySqlParameter[])

      • Task<DataSet> ExecuteDatasetAsync(MySqlConnection, string, CancellationToken, params MySqlParameter[])

      • Task UpdateDataSetAsync(string, string, DataSet, string)

      • Task UpdateDataSetAsync(string, string, DataSet, string, CancellationToken)

      • Task<MySqlDataReader> ExecuteReaderAsync(MySqlConnection, MySqlTransaction, string, MySqlParameter[], bool)

      • Task<MySqlDataReader> ExecuteReaderAsync(MySqlConnection, MySqlTransaction, string, MySqlParameter[], bool, CancellationToken)

      • Task<MySqlDataReader> ExecuteReaderAsync(string, string)

      • Task<MySqlDataReader> ExecuteReaderAsync(string, string, CancellationToken)

      • Task<MySqlDataReader> ExecuteReaderAsync(MySqlConnection, string)

      • Task<MySqlDataReader> ExecuteReaderAsync(MySqlConnection, string, CancellationToken)

      • Task<MySqlDataReader> ExecuteReaderAsync(string, string, params MySqlParameter[])

      • Task<MySqlDataReader> ExecuteReaderAsync(string, string, CancellationToken, params MySqlParameter[])

      • Task<MySqlDataReader> ExecuteReaderAsync(MySqlConnection, string, params MySqlParameter[])

      • Task<MySqlDataReader> ExecuteReaderAsync(MySqlConnection, string, CancellationToken, params MySqlParameter[])

      • Task<object> ExecuteScalarAsync(string, string)

      • Task<object> ExecuteScalarAsync(string, string, CancellationToken)

      • Task<object> ExecuteScalarAsync(string, string, params MySqlParameter[])

      • Task<object> ExecuteScalarAsync(string, string, CancellationToken, params MySqlParameter[])

      • Task<object> ExecuteScalarAsync(MySqlConnection, string)

      • Task<object> ExecuteScalarAsync(MySqlConnection, string, CancellationToken)

      • Task<object> ExecuteScalarAsync(MySqlConnection, string, params MySqlParameter[])

      • Task<object> ExecuteScalarAsync(MySqlConnection, string, CancellationToken, params MySqlParameter[])

    • MySqlScript

      • Task<int> ExecuteAsync()

      • Task<int> ExecuteAsync(CancellationToken)

除了上面列出的方法外,以下是从 .NET Framework 继承的方法

  • 命名空间 MySql.Data.Entity

    • EFMySqlCommand

      • Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehaviour, CancellationToken)

      • Task<int> ExecuteNonQueryAsync()

      • Task<int> ExecuteNonQueryAsync(CancellationToken)

      • Task<DbDataReader> ExecuteReaderAsync()

      • Task<DbDataReader> ExecuteReaderAsync(CancellationToken)

      • Task<DbDataReader> ExecuteReaderAsync(CommandBehaviour)

      • Task<DbDataReader> ExecuteReaderAsync(CommandBehaviour, CancellationToken)

      • Task<object> ExecuteScalarAsync()

      • Task<object> ExecuteScalarAsync(CancellationToken)

  • 命名空间 MySql.Data

    • MySqlCommand

      • Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehaviour, CancellationToken)

      • Task<int> ExecuteNonQueryAsync()

      • Task<int> ExecuteNonQueryAsync(CancellationToken)

      • Task<DbDataReader> ExecuteReaderAsync()

      • Task<DbDataReader> ExecuteReaderAsync(CancellationToken)

      • Task<DbDataReader> ExecuteReaderAsync(CommandBehaviour)

      • Task<DbDataReader> ExecuteReaderAsync(CommandBehaviour, CancellationToken)

      • Task<object> ExecuteScalarAsync()

      • Task<object> ExecuteScalarAsync(CancellationToken)

    • MySqlConnection

      • Task OpenAsync()

      • Task OpenAsync(CancellationToken)

    • MySqlDataReader

      • Task<T> GetFieldValueAsync<T>(int)

      • Task<T> GetFieldValueAsync<T>(int, CancellationToken)

      • Task<bool> IsDBNullAsync(int)

      • Task<bool> IsDBNullAsync(int, CancellationToken)

      • Task<bool> NextResultAsync()

      • Task<bool> NextResultAsync(CancellationToken)

      • Task<bool> ReadAsync()

      • Task<bool> ReadAsync(CancellationToken)

示例

以下 C# 代码示例演示了如何使用异步方法

在本示例中,一个方法具有 async 修饰符,因为方法对 LoadAsync 方法进行了 await 调用。该方法返回一个 Task 对象,其中包含有关已等待方法的结果的信息。返回 Task 就像有一个 void 方法一样,但如果您的方法不是顶层访问方法(例如事件),则不应使用 async void

public async Task BulkLoadAsync()
{
  MySqlConnection myConn = new MySqlConnection("MyConnectionString");
  MySqlBulkLoader loader = new MySqlBulkLoader(myConn);

  loader.TableName       = "BulkLoadTest";
  loader.FileName        = @"c:\MyPath\MyFile.txt";
  loader.Timeout         = 0;

  var result             = await loader.LoadAsync();
}

在本示例中,一个 "async void" 方法与 "await" 一起用于 ExecuteNonQueryAsync 方法,以对应于按钮的 onclick 事件。这就是为什么该方法不返回 Task

private async void myButton_Click()
{
  MySqlConnection myConn = new MySqlConnection("MyConnectionString");
  MySqlCommand proc      = new MySqlCommand("MyAsyncSpTest", myConn);

  proc.CommandType       = CommandType.StoredProcedure;

  int result             = await proc.ExecuteNonQueryAsync();
}