等待/异步微软实践企业库数据 [英] await/async Microsoft Practices Enterprise Library Data

查看:205
本文介绍了等待/异步微软实践企业库数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个旧的应用程序,我写的,我用Microsoft.P​​ractices.EnterpriseLibrary.Data以从DB数据。我最近升级到.NET 4.5,想要利用计谋/异步的。但是,我没有看到任何方式结束的异步按照命名标准,即使是在最新版本的软件包。是否有可能使用计谋/异步使用而无需手动使这个ADO库是异步的?

So, 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. But, 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 library without manually making it asynchronous?

推荐答案

其实,我能找到的异步方法。我只是在错误的地点。以下是访问数据库异步两种常见的方式:

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);
                }
            }
        }

您可以使用 GetFieldValueAsync< T> 而不是 GetInt32等如果您使用的是的CommandBehavior .SequentialAccess 用大量的数据。但是,在大多数情况下,你也许并不需要做到这一点。

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.

这篇关于等待/异步微软实践企业库数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆