Search code examples
c#ado.netasync-awaitenterprise-libraryasp.net-4.5

await/async Microsoft Practices Enterprise Library Data


I have an older application that I wrote where I used Microsoft.Practices.EnterpriseLibrary.Data to get data from the DB. I have recently upgraded to .NET 4.5 and wanted to advantage of await/async.

I do not see any methods ending in "Async" as per the naming standard, even in the most recent version of the package. Is it possible to use await/async with this ADO .NET library without manually making it asynchronous?


Solution

  • I actually was able to find the Async methods. I was just looking in the wrong spots. Here's two common ways of accessing the DB asynchronously:

    var db = DatabaseFactory.CreateDatabase(GlobalConstants.DBConnection);
    using (var cmd = db.GetStoredProcCommand("SprocName", parameterA))
    {
        await cmd.ExecuteNonQueryAsync();
    }
    

    and when you want to get data:

    var db = DatabaseFactory.CreateDatabase(GlobalConstants.DBConnection);
    using (var cmd = db.GetStoredProcCommand("SprocName", parameterA, parameterB, parameterC))
    {
        using (var dr = await cmd.ExecuteReaderAsync())
        {
            while (await dr.ReadAsync())
            {
                return dr.GetInt32(0);
            }
        }
    }
    

    You can use GetFieldValueAsync<T> instead of GetInt32 if you are using CommandBehavior.SequentialAccess with large amounts of data. But, for most cases, you probably do not need to do this.