views:

81

answers:

2

Hi I am bringing about 100000 records into memory cleaning the data and inserting into a new table. After inserting around 2000 records I get the following exception.

A first chance exception of type 'System.OutOfMemoryException' occurred in Iesi.Collections.DLL A first chance exception of type 'FluentNHibernate.Cfg.FluentConfigurationException' occurred in FluentNHibernate.DLL

I am using Fluent NHibernate. but am not sure if the problem is related to fluent NHibernate itself.

What is the best way to do bulk inserts to the db, I am wondering whether to use stringbuilder and build an sql query instead. Any pointers much appreciated.

+1  A: 

Try using a stateless session:

object[] objectsToInsert = GetObjectsToInsert();

using (var statelessSession = sessionFactory.OpenStatelessSession())
using (var transaction = statelessSession.BeginTransaction())
{
    foreach (var objectToInsert in objectsToInsert)
    {
        statelessSession.Insert(objectToInsert);
    }
    transaction.Commit();
}
Darin Dimitrov
Thanks, same error occurs so it must be somewhere else.
Chin
+3  A: 

Whilst I'm sure that there are many techniques that you can employ to do this kind of operation through hibernate, it seems likely that you would be better off constructing your data-cleansing operation as an SQL statement and running it on the database itself. If you need to run it regularly, you might consider a stored procedure.

Tom
Point taken, I really want to do this in C# though - as with the kind of data-cleansing I am doing, I wouldn't know where to even start with building the correct sql statement. Thanks
Chin
There are plenty of keen SQL experts out there - you probably have one at your company - but if you can't find one there are lots on stack overflow :-)
Tom
@Tom One again appreciated.
Chin