views:

289

answers:

2

Hi there,

I'm writing a windows service that import an XML file into a SQLite database.

There are 3,000 odd records that need to be created and i'm using SubSonic 2.2 for the project.

Instead of looping through a list and adding them to the database one by one is there a way to batch query more than 1 new record at a time.

I know the "BatchQuery" object in 3.x would help me here but i was hoping there was something in 2.x that did a similar task.

thanks in advance Doug

+2  A: 

SubSonic 2.x does have a method to perform batch saves.

Here is a sample:

var itemsToSaveCollection = new ItemCollection(); // Your collection type here

foreach (var xmlItem in xmlItems)
{
    var item = new Item(); // Your data model type here
    // Set item values from xml
    itemsToSaveCollection.Add(item);
}

itemsToSaveCollection.BatchSave();
Marve
does this make ONE database call?
Doug
It does indeed make one database call. Check out lines 98 through 109 of the SubSonic [source](http://www.koders.com/csharp/fidD7B3D21941459E52F4672CEBD784AA5A32F1E48D.aspx?s=datatable)
Marve
Love your work marve - after doing it the ugly way for the proof of concept (it took 10 mins to complete) - this is great as this is how i wanted it done - 2 seconds to complete.
Doug
Awesome. Glad I could help.
Marve
A: 

Will BatchSave() know whether to INSERT or UPDATE?

Sometimes you have items in a database already will these just get updated and new ones inserted or is BatchSave() purely for multiple inserts at once?

DotnetShadow