Search code examples
c#sqlitec#-3.0database-restore

How to restore a SQLite database while an open connection exists?


I am writing a plugin for a C# application which uses SQLite database. The initial database is stored in database.db.default. At any point during runtime the user should be able to reset the database to the default content.

Since the application has always an open connection to the database I can not just copy database.db.default to database.db.

Whether or not it is a good idea to always hold an open connection is a total different question. But that is just how it is since it is not my call to change that part of the application.

Is there any way I am able to restore database.db.default into database.db using C# and .NET 3.0?

I appreciate any kind of help ...


Solution

  • If you can't do a file copy to restore the database then do the restore the hard way. Attach the backup database, delete all the data in the main database, then insert the data from the backup database.