I have the following (simplified) code:
IDbConnection connection = new SQLiteConnection(GetConnectionString());
if (connection.State == ConnectionState.Closed)
connection.Open();
using (IDbTransaction transaction = connection.BeginTransaction())
{
using (IDbCommand cmd = transaction.Connection.CreateCommand())
{
cmd.CommandText = "DELETE FROM Records2 WHERE ClientIndex = @ClientIndex";
AddParameter(cmd, "@ClientIndex", DbType.Int32, clientIndex);
cmd.ExecuteNonQuery();
}
using (IDbCommand cmd = transaction.Connection.CreateCommand())
{
cmd.CommandText = "INSERT INTO Records2(ClientIndex, CandidateIndex, Name)";
cmd.CommandText += " VALUES(@ClientIndex, @CandidateIndex, @Name)";
AddParameter(cmd, "@ClientIndex", DbType.Int32, clientIndex);
IDbDataParameter pIndex = AddParameter(cmd, "@CandidateIndex", DbType.Int32, null);
IDbDataParameter pName = AddParameter(cmd, "@Name", DbType.AnsiString, null);
int index = 0;
foreach (Record record in records)
{
pIndex.Value = index++;
pName.Value = record.Name;
cmd.ExecuteNonQuery();
}
}
using (IDbCommand cmd = transaction.Connection.CreateCommand())
{
cmd.CommandText = "UPDATE Records SET Status = @Status, UpdateDate = @UpdateDate WHERE ClientIndex = @ClientIndex";
AddParameter(cmd, "@ClientIndex", DbType.Int32, clientIndex);
AddParameter(cmd, "@Status", DbType.Byte, status);
AddParameter(cmd, "@UpdateDate", DbType.DateTime, DateTime.Now);
cmd.ExecuteNonQuery();
}
transaction.Commit();
}
connection.Close();
with:
private IDbDataParameter AddParameter(IDbCommand command, string paramName, DbType type, object value)
{
IDbDataParameter parameter = command.CreateParameter();
parameter.ParameterName = paramName;
parameter.DbType = type;
if (value != null)
parameter.Value = value;
else
parameter.Value = DBNull.Value;
command.Parameters.Add(parameter);
return parameter;
}
All the ExecuteNonQueries work without problems, except for the last one. On my machine (running xp) I get the following exception, but on other machines (running windows 7) it works without problems, with the same code and the same database file.
SQLiteException (0x80004005): Unable to open the database file
System.Data.SQLite.SQLite3.Reset(SQLiteStatement stmt) +375
System.Data.SQLite.SQLite3.Step(SQLiteStatement stmt) +199
System.Data.SQLite.SQLiteDataReader.NextResult() +226
System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave) +87
System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior) +38
System.Data.SQLite.SQLiteCommand.ExecuteNonQuery() +39
I tried using JournalMode = Perist in the connection string and i excluded the directory containing the database file from my anti-virus, but nothing helps.
I'm using the 3.5 Framework and my version of System.Data.SQLite is 1.0.66.0.
Any Ideas ?
Thanks in advance,
Marc