简体   繁体   中英

Add lots of data to SQLite Database in C#

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);
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM