views:

32

answers:

1

I have a list of items that I want to insert into the database but I do not want any duplicates. So I delete all the items and reinsert all. It's not the most efficient way but it's not many items so it works for me. But I get syntax error near "Insert". I do the following:

DELETE FROM Settings WHERE Type = 'Extensions'
INSERT INTO Settings (Value, Type) Values ('img', 'Extensions')
INSERT INTO Settings (Value, Type) Values ('avi', 'Extensions')
INSERT INTO Settings (Value, Type) Values ('mpg', 'Extensions')
INSERT INTO Settings (Value, Type) Values ('mkv', 'Extensions')
INSERT INTO Settings (Value, Type) Values ('mov', 'Extensions')
INSERT INTO Settings (Value, Type) Values ('iso', 'Extensions')

And execute it with this:

protected bool ExecuteCommand(string command, bool rollbackOnError)
{
    using (SQLiteTransaction transaction = DbConnection.BeginTransaction())
    {
        try
        {

            using (SQLiteCommand mycommand = new SQLiteCommand(DbConnection))
            {
                mycommand.CommandText = command;
                mycommand.ExecuteNonQuery();
                transaction.Commit();
            }
        }
        catch
        {
            if (rollbackOnError)
            {
                transaction.Rollback();
            }
            return false;
        }
    }
    return true;
}
+2  A: 

You need to separate your commands with a semicolon, don't you?

Also I am not sure if the command is able to run multiple statements.

Aliostad
Thanks, can't believe I missed that. It can run multiple statements though
Oskar Kjellin
Sure, it just cannot accept GO. I remember having a problem with SQL Server not being able to execute multiple statements but can be wrong... ages since touched database stuff without ORM.
Aliostad