sqlite - any improvements for this attach code (running multiple sql commands transactionally in sql

Posted by Greg on Stack Overflow See other posts from Stack Overflow or by Greg
Published on 2010-03-18T02:02:09Z Indexed on 2010/03/18 3:01 UTC
Read the original article Hit count: 628

Filed under:
|
|
|
|

Hi,

Is this code solid? I've tried to use "using" etc. Basically a method to pass as sequenced list of SQL commands to be run against a Sqlite database.

I assume it is true that in sqlite by default all commands run in a single connection are handled transactionally? Is this true? i.e. I should not have to (and haven't got in the code at the moment) a BeginTransaction, or CommitTransaction.

It's using http://sqlite.phxsoftware.com/ as the sqlite ADO.net database provider.

private int ExecuteNonQueryTransactionally(List<string> sqlList)
{
    int totalRowsUpdated = 0;

    using (var conn = new SQLiteConnection(_connectionString))
    {
        // Open connection (one connection so should be transactional - confirm)
        conn.Open();

        // Apply each SQL statement passed in to sqlList
        foreach (string s in sqlList)
        {
            using (var cmd = new SQLiteCommand(conn))
            {
                cmd.CommandText = s;
                totalRowsUpdated = totalRowsUpdated + cmd.ExecuteNonQuery();
            }
        }
    }

    return totalRowsUpdated;
}

© Stack Overflow or respective owner

Related posts about sqlite

Related posts about c#