简体   繁体   English

如何实现异步任务以使用异步和等待从数据库中获取数据?

[英]How to implement Async Task to fetch data from database using async and await?

I am doing this. 我正在做。 It's working but is this a recommended way to do this. 它正在工作,但这是推荐的方法。 Please comments 请留言

public async void LoadData()
{
    DataTable  dtAdditionsDetails = await LoadReportData(importID,
                                             InkeyCommon.ToInt32(cmbSellers.SelectedValue),
                                             fromDate,
                                             toDate);
    if (dtAdditionsDetails != null)
      dtaGrdAdditions.ItemSource = dtAdditionsDetails.DefaultView;
} 

public async Task<DataTable> LoadReportData(int? importID,
                                        int sellerID,
                                        DateTime? fromDate,
                                        DateTime? toDate)
{
    DataTable dtAdditionsDetails = new DataTable();

    //Get Report Data                                             
    await Task.Delay(1);

    dtAdditionsDetails = ReportsData.GetRptAdditions(importID,
                                  sellerID,
                                  fromDate,
                                  toDate);

    return dtAdditionsDetails;
}

In order to use the await keyword properly, the object being 'awaited' should really be an ...Async method, like the GetStringAsync method. 为了正确使用await关键字,被“等待”的对象实际上应该是...Async方法,例如GetStringAsync方法。 As @ken2k has correctly pointed out, you cannot just await any method. 正如@ ken2k正确指出的那样,您不能仅await任何方法。 Therefore, to answer your question is this a recommended way to do this? 因此,要回答您的问题,这是推荐的方法吗? , the answer is no. , 答案是不。

You can find out how to use the await and async keywords correctly in the Asynchronous Programming with Async and Await (C# and Visual Basic) page on MSDN, however, if you're just trying to run a synchronous method asynchronously, then you can do that like this: 您可以在MSDN上的Async and Await异步编程(C#和Visual Basic)页面中找到如何正确使用awaitasync关键字,但是,如果您只是尝试异步运行同步方法,则可以像这样:

public DataTable LoadData()
{
    DataTable dtAdditionsDetails = ...
    // Get your data here synchronously
    return dtAdditionsDetails;
} 

public async Task<DataTable> LoadDataAsync()
{
    DataTable dtAdditionsDetails = LoadData();
    return Task.Run(() => LoadData());
} 

... ...

public async void GetDataAsynchronously()
{
    DataTable dtAdditionsDetails = await LoadDataAsync();
} 

Note that ...Async methods (usually) return Task<T> objects, rather than nothing and that their names end in the word Async . 请注意, ...Async方法(通常)返回Task<T>对象,而不是返回任何对象,并且它们的名称以单词Async结尾。 Also note that only the data is returned from ...Async methods and not the Task<T> and that we don't await when there is nothing to await . 另外请注意,只有数据从返回...Async方法,而不是 Task<T>和我们 await时,有什么可await

Here's a simple function, to load a list of all [User] records from a database, using async and await . 这是一个简单的函数,可以使用asyncawait从数据库加载所有[User]记录的列表。

[HttpGet]
public async Task<IActionResult> Get()
{
    try
    {
        //  Fetch a list of all the [User] records, just for the hell of it.
        var users = await Task.Run(() => dbContextWebMgt.Users.ToList());
        return new OkObjectResult(users);
    }
    catch (Exception ex)
    {
        return new BadRequestObjectResult(ex.Message);
    }
}

You can try like this using NPoco ORM 您可以使用NPoco ORM这样尝试

[HttpGet]
public async Task<IActionResult> GetAsync()

{
      IList myList = await FetchAsync();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM