Does .NET 4.5's async feature work with MySql and other databases too?

后端 未结 3 409
日久生厌
日久生厌 2020-12-21 00:33

I understand that .NET 4.5 comes with a bunch of features to make asynchronous database operations easier to implement. MSDN says that if the connection string is not set to

3条回答
  •  渐次进展
    2020-12-21 01:18

    As Panagiotis Kanavos mentioned DbDataReader provides ReadAsync method signatures, however not all drivers support this. Some, like the MySql 6.9.5 driver, implement them synchronously.

    To answer your more general question, if a (No)SQL driver does NOT inherently support *Async methods that are awaitable, but it does have "APM" IAsyncResult based methods (e.g. BeginRead.. EndRead...), then you can wrap those up using Task.Factory.FromAsync. Here is an example for MySql:-

    public static class MySqlCommandExtension
    {
        public static Task MyExecuteReaderAsync(this MySqlCommand source, CommandBehavior behavior = CommandBehavior.Default)
        {
            return Task.Factory.FromAsync(source.BeginExecuteReader(behavior), source.EndExecuteReader);
        }
    }
    

    This pattern is descibed in more detail on MSDN.

提交回复
热议问题