gpt4 book ai didi

c# - 在 C# 中向 SQLite 数据库添加大量数据

转载 作者:搜寻专家 更新时间:2023-10-30 21:51:34 24 4
gpt4 key购买 nike

我目前正在尝试使用 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);
}
}

最佳答案

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

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

关于c# - 在 C# 中向 SQLite 数据库添加大量数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15671600/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com