加入大量数据到SQLite数据库在C# [英] Add lots of data to SQLite Database in C#
问题描述
我目前正在尝试将大量的数据在我的Windows 8应用使用C#和System.Data.SQLite添加到SQLite数据库。我有一个列表中的所有对象(MediaItem),我使用foreach循环将每一个数据库。 Unfortunatley这是相当缓慢的,所以我不知道是否有一种方法可以加快速度。我能为例子交出完整列表到数据库?
这是我到目前为止的代码:
列表< MediaItem>项目;
//添加许多onbjects到列表...
使用(VAR DB =新SQLiteConnection(DBPATH))
{
的foreach(在项目VAR项目)
{
db.Insert(项目);
}
}
从我的经验,包装尽可能多的数据库调用尽可能到一个事务中加快东西相当多的:
使用(VAR DB =新SQLiteConnection(DBPATH))
{
db.RunInTransaction(()=>
{
的foreach(在项目VAR项目)
{
分贝。插入(项目);
}
});
}
I'm currently trying to add a lot of data to a SQLite database in my Windows 8 App using C# and System.Data.SQLite. I have all the objects (MediaItem) in a list and I'm adding each one to the database using a foreach-loop. Unfortunatley this is pretty slow, so I'm wondering if there's a way to speed things up. Can I for example hand over the complete list to the database? Here's my code so far:
List<MediaItem> items;
// adding lots of onbjects to the list...
using (var db = new SQLiteConnection(dbPath))
{
foreach (var item in items)
{
db.Insert(item);
}
}
From my experience, wrapping as many database calls as possible into a transaction speeds things up quite a bit:
using (var db = new SQLiteConnection(dbPath))
{
db.RunInTransaction(() =>
{
foreach (var item in items)
{
db.Insert(item);
}
});
}
这篇关于加入大量数据到SQLite数据库在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!