I use asp.net C # and upload the SqLite database to the server, and then I insert and update. The problem is that sometimes (I think when something goes wrong with the update or so), the database is locked. Therefore, the next time I try to download the file again, it is blocked, and I get the error message "The process cannot access the file because it is being used by another process." Maybe the database file will not be deleted if something goes wrong during the transaction? The only thing that can be solved is to restart the server.
How can I solve this in my code so that I can be sure that it is always unlocked, even if something goes wrong?
This is my code:
try
{
string filepath = Server.MapPath("~/files/db.sql");
((HttpPostedFile)HttpContext.Current.Request.Files["sqlitedb"]).SaveAs(filepath);
SQLiteConnection conn = new SQLiteConnection("Data Source=" + filepath + ";Version=3;");
conn.Open();
SQLiteCommand cmd = new SQLiteCommand(conn);
using (SQLiteTransaction transaction = conn.BeginTransaction())
{
using (cmd)
{
}
transaction.Commit();
}
conn.Close();
cmd.Dispose();
}
catch (Exception exp)
{
}
Martin