【问题标题】:Add lots of data to SQLite Database in C#在 C# 中向 SQLite 数据库添加大量数据
【发布时间】:2013-03-27 23:38:30
【问题描述】:

我目前正在尝试使用 C# 和 System.Data.SQLite 将大量数据添加到我的 Windows 8 应用程序中的 SQLite 数据库中。我有一个列表中的所有对象(MediaItem),我正在使用 foreach 循环将每个对象添加到数据库中。不幸的是,这很慢,所以我想知道是否有办法加快速度。例如,我可以将完整列表交给数据库吗? 到目前为止,这是我的代码:

List<MediaItem> items;

// adding lots of onbjects to the list...

using (var db = new SQLiteConnection(dbPath))
{
     foreach (var item in items)
     {
         db.Insert(item);
     }
 }

【问题讨论】:

标签: c# database sqlite windows-store-apps


【解决方案1】:

根据我的经验,将尽可能多的数据库调用包装到一个事务中会大大加快速度:

using (var db = new SQLiteConnection(dbPath))
{
    db.RunInTransaction(() =>
    {
        foreach (var item in items)
        {
            db.Insert(item);
        }
    });
}

【讨论】:

  • 就像一个魅力,非常感谢。现在添加 50.000 个项目只需不到一秒钟即可完成,而之前需要几分钟。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-25
  • 1970-01-01
相关资源
最近更新 更多