Search code examples
c#sqlitedataadapter

SQLite DataAdapter UpdateBatchSize Error


Background:

I am attempting to copy a DataTable to a SQLite database. In comparison to an equivalent data set running on a SQLServer database, the SQLite version takes about 5 times longer. In order to streamline this, I am trying to use the UpdateBatchSize property of the SQLiteDataAdapter I'm using to enable batch processing.

Problem:

However, any attempt to set the value results in a System.NotSupportedException. How do you properly set that value? See code:

public int InsertDataTable(DataTable dt, string selectCommand)
{
    SQLiteDataAdapter myAdapter = new SQLiteDataAdapter(selectCommand, m_conn);
    myAdapter.UpdateBatchSize = 0;
    int rowsAffected = myAdapter.Update(dt.Select());
    return rowsAffected;
}

Solution

  • The same performance increase can be accomplished through another method. Instead of trying to modify the UpdateBatchSize, create a SQLiteTransaction that encompasses the update.

    public int InsertDataTable(DataTable dt, string selectCommand)
        {
            SQLiteDataAdapter adapter = new SQLiteDataAdapter(selectCommand, m_conn);
            SQLiteCommandBuilder builder = new SQLiteCommandBuilder(adapter);
            SQLiteTransaction transaction = m_conn.BeginTransaction();
            builder.GetInsertCommand().Transaction = transaction;
            int rowsAffected = adapter.Update(dt.Select());
            transaction.Commit();            
            return rowsAffected;
        }
    

    Source:

    SQLiteTransction