保存数据库记录或异步部件并联在.NET C# [英] Save Records in Database asynchronously Or Parallely in .net c#
问题描述
我在我从一个API后,获得了大量的记录保存到数据库的服务。
同时我有这些记录从服务中返回给调用者。
但问题是,我节省了DB的记录是需要长时间使服务越来越慢。
我已搜查这个发现并行任务或异步&放一些conecept;等待着。
I have a service in which i have to save a large number of records to database after getting from an API. At the same time i have to return those records to from service to the caller. But problem is that i saving records in DB is taking long time so Service is getting slow. I have searched about this and found some conecept of Parallel task OR async & await.
我是新来这个概念和它的使用感到困惑
I am new to this concept and getting confused in its usage
我看着:
运行多个C#任务异步
http://msdn.microsoft.com/en-us/library/hh191443.aspx
但我没有得到以do.Please什么帮助我:
But i am not getting what to do.Please help me:
下面是code:
public List<SearchedItems> SearchItems(string ItemToSearch, string AuthenticationToken)
{
var _list= getRecords from Api //100 records
//Task<int>.Factory.StartNew(() => _objBLLNutritionLog.FillNutritionTable(_tempList)); // also tried this
saveToDb(_list); // need to run this asynchronously Or parallel (Taking long time)
return _list;
}
我要回结果主叫方和其他方要填写分贝。
请建议。
i want to return result to caller and on other side want to fill db. Please suggest.
感谢您
推荐答案
我希望你正在使用.NET 4.5
I hope you are using .Net 4.5
所以,让我们开始吧。首先标记您SearchItems功能与异步:
So let's get started. First mark your SearchItems function with async:
public async List<SearchedItems> SearchItems(string ItemToSearch, string AuthenticationToken)
{
}
然后就可以等待的saveToDb任务:
Then you can await for saveToDb task:
var result = await Task.Factory.StartNew(()=> saveToDb(_list));
希望这有助于!
这篇关于保存数据库记录或异步部件并联在.NET C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!