2014-12-02 53 views
1

我將記錄添加到一個Azure數據使用下面的方法在按鈕click.But我想知道是否有更好的方式來提交數據到數據庫的性能和可擴展性方面沒有爲每個新的項目記錄?如何異步提交多個記錄到數據庫?

這是我當前如何將數據提交到Azure的移動服務:

  Item itemOne = new Item { Repititions = " " + fingersSpreadScoreCntr.ToString(), Date = " " + DateTime.Now.ToString(@"MM\/dd\/yyyy h\:mm tt"), User = "Ted Bundy" , Exercise = "Fingers Spread"}; 
      await App.MobileService.GetTable<Item>().InsertAsync(itemOne); 

      Item itemTwo = new Item { Repititions = " " + fistHeldScoreCntr.ToString(), Date = " " + DateTime.Now.ToString(@"MM\/dd\/yyyy h\:mm tt"), User = "Joe Bloggs", Exercise = "Fist Held" }; 
      await App.MobileService.GetTable<Item>().InsertAsync(itemTwo); 

      Item itemThree = new Item { Repititions = " " + waveOutScoreCntr.ToString(), Date = " " + DateTime.Now.ToString(@"MM\/dd\/yyyy h\:mm tt"), User = "George Bush", Exercise = "Wrist Extension" }; 
      await App.MobileService.GetTable<Item>().InsertAsync(itemThree); 
+0

你是什麼意思「*提交數據到數據庫,而每個新紀錄*創建新項目」,批量插入是什麼意思? – b2zw2a 2014-12-02 21:53:16

回答

2

據我所知沒有散裝移動業務表中插入功能。 可能會加速多個插入的一件事是異步並行地執行它們。在您的代碼示例中,您在等待(await)每InsertAsync完成,然後開始下一個。調用所有插入操作會更快,然後等待它們全部完成。示例代碼可能如下所示:

 var table = App.MobileService.GetTable<Item>(); 
     Item itemOne = new Item { Repititions = " " + fingersSpreadScoreCntr.ToString(), Date = " " + DateTime.Now.ToString(@"MM\/dd\/yyyy h\:mm tt"), User = "Ted Bundy", Exercise = "Fingers Spread" }; 
     Item itemTwo = new Item { Repititions = " " + fistHeldScoreCntr.ToString(), Date = " " + DateTime.Now.ToString(@"MM\/dd\/yyyy h\:mm tt"), User = "Joe Bloggs", Exercise = "Fist Held" }; 
     Item itemThree = new Item { Repititions = " " + waveOutScoreCntr.ToString(), Date = " " + DateTime.Now.ToString(@"MM\/dd\/yyyy h\:mm tt"), User = "George Bush", Exercise = "Wrist Extension" }; 

     await Task.WhenAll(
      table.InsertAsync(itemOne), 
      table.InsertAsync(itemTwo), 
      table.InsertAsync(itemThree)); 

這樣您還可以在每次插入後消除不必要的上下文切換。

Why should I prefer single 'await Task.WhenAll' over multiple awaits?