在SQLite中插入从webservice返回的大型记录的最佳方法是什么

在SQLite中插入从webservice返回的大型记录的最佳方法是什么,sqlite,windows-8,windows-runtime,microsoft-metro,winrt-xaml,Sqlite,Windows 8,Windows Runtime,Microsoft Metro,Winrt Xaml,在WinRT(Win8)中使用基于异步的Webservice和异步框架从远程Ms SQL Server获取大型记录集(1000到5000) 我想知道: 1) 将大型记录集插入SQLite的最佳处理方法是什么? 2) 如果存在连接错误,使用回滚事务将重新开始。下面的方法将插入任何内容,如果记录不完整,我可以稍后更新数据。这是一个好方法吗? 3) 有没有更好的方法来增强我的以下解决方案 This foreach statement to handle each reords in returned

在WinRT(Win8)中使用基于异步的Webservice和异步框架从远程Ms SQL Server获取大型记录集(1000到5000)

我想知道:

1) 将大型记录集插入SQLite的最佳处理方法是什么?
2) 如果存在连接错误,使用回滚事务将重新开始。下面的方法将插入任何内容,如果记录不完整,我可以稍后更新数据。这是一个好方法吗?
3) 有没有更好的方法来增强我的以下解决方案

This foreach statement to handle each reords in returned result which returned from Async-Based WebService: foreach (WebServiceList _List in IList) { InsertNewItems(_List.No, _List.Description, _List.Unit_Price, _List.Base_Unit_of_Measure); } private void InsertNewItems(string ItemNo, string ItemName, decimal ItemPrice, string ItemBUoM) { var existingItem = (db2.Table().Where(c => c.No == ItemNo)).SingleOrDefault(); if (existingItem != null) { existingItem.No = ItemNo; existingItem.Description = ItemName; existingItem.Unit_Price = ItemPrice; existingItem.BaseUnitofMeasure = ItemBUoM; int success = db2.Update(existingItem); } else { int success = db2.Insert(new Item() { No = ItemNo, Description = ItemName, Unit_Price = ItemPrice, Base_Unit_of_Measure = ItemBUoM }); } } 这是要处理的foreach语句 从基于异步的Web服务返回的返回结果中的每个reords: foreach(IList中的WebServiceList_列表) { 插入新项目(\u List.No、\u List.Description、\u List.Unit\u Price、\u List.Base\u度量单位); } 私有void InsertNewItems(string ItemNo、string ItemName、decimal ItemPrice、string ItemBUoM) { var existingItem=(db2.Table().Where(c=>c.No==ItemNo)).SingleOrDefault(); 如果(existingItem!=null) { existingItem.No=ItemNo; existingItem.Description=ItemName; existingItem.Unit_Price=ItemPrice; existingItem.BaseUnitofMeasure=ItemBUoM; int success=db2.Update(existingItem); } 其他的 { int success=db2.Insert(新项() { 否=项目否, Description=项目名称, 单价=项目价格, 基本度量单位=ItemBUoM }); } }
批量插入最重要的性能方面是使用单个事务。如果您想处理中止,我建议您将数据分成足够大的部分,并在下次从该点重新启动。SQL事务要么完全完成,要么完全回滚,因此除非输入数据在两次运行之间发生更改,否则不需要执行插入或更新


例如,有关使用不同方法的SQLite大容量插入性能的讨论,请参见。

您应该使用中的
RunInTransaction
。文件上说

通过包装在事务(可能是嵌套的)中执行操作 在保存点中。如果发生异常,则滚动整个事务 返回,而不仅仅是当前保存点。例外情况是重新启动


对于GitHub上的事务

大容量插入通常会禁用事务。一旦遇到错误,此语句将使用(var db=new SQLiteConnection())停止SQLite。我应该将SQLite声明为一个全局变量,这样即使遇到错误,我也可以继续运行它吗?
using (var db = new SQLiteConnection(DbPath))
{
    db.RunInTransaction(() =>
    {
        db.InsertOrReplace(MyObj);
    });
}