1

I need insert to large amount of data to sqlite db.
I uses Linq to Entities.

I have problem to adding large amount of data 1M+.
Not enough memory or a very long time.

This code - fast, but requires a lot of memory:

// query - IQueryable of DbfRecord
// db - ObjectContext
int i = 0;
foreach (var item in query) {
    db.AddToKladrs(new Kladr() {
        Id = item.GetField(0),
        ParentId = item.GetField(1),
        RegionId = item.GetField(3),
        Name = item.GetField(2),
        Index = item.GetField(4)
    });
    if(++i % 4000 == 0)
        db.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
}


This code - not resource-intensive, but very slow:

// query - IQueryable of DbfRecord
// db - ObjectContext
foreach (var item in query) {
    db.ExecuteStoreCommand("insert into [Kladr] values({0}, {1}, {2}, {3}, {4})",
        item.GetField(0),
        item.GetField(1),
        item.GetField(3),
        item.GetField(2),
        item.GetField(4)
    );
}

I missed the try-catch construction and ghost types.

Help me find the best solution!

Community
  • 1
  • 1
Artem Zubkov
  • 559
  • 1
  • 14
  • 29

1 Answers1

1

You can use the SqlBulkCopy for copying large amounts of data. Havn't tried it with SQL lite but it should work.

Link 1
Link 2

Update :

Here is a good answer by Marc Gravell. how-to-do-a-bulk-insert-linq-to-entities

Community
  • 1
  • 1
Jethro
  • 5,896
  • 3
  • 23
  • 24
  • this don't work with `DbConnection`. I cann't conver `SQLiteConnection` (`DbConnection`) to `SqlConnection` =( i uses `System.Data.SQLite` – Artem Zubkov Jul 24 '11 at 10:20
  • I just found that Bulk insert is not supported by sqlite, however you can use a transaction which will do the same thing. – Jethro Jul 24 '11 at 15:22