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

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 ...

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.

I'm probably missing something here, but can't you just close your connection and reopen it after the copy?

Related

Xamarin.Forms Android app SQLite data disappears

I am writing a Xamarin.Forms app for Android using C#. I setup the Sqlite dB at: System.Environment.SpecialFolder.Personal
When the app is deployed on device I can add data and then retrieve it.
However, when I check the app next day(offline, without debugging from pc) all the dB data is gone.
Does it mean that the dB file is gone after some time?
Data from DB should not gone.
Are your sure that your DB initialization code is ok? Maybe every time you started the app, tha data is restoring to some start state?
The mysterious dissappearance of data in a sqlite db can have multiple causes.
Make sure that your db file exists, maybe you can implement a small debug button somwhere which will throw an alert which tells you if the file exists.
However using System.Environment.SpecialFolder.Personal shouldn't be the cause, I use that folder for my apps as well.
Second, do you actively commit your transactions? Especially when your app gets backgrounded, i would recommend calling Commit() in order to make sure that every pending transaction gets written into database, since you never know when the garbage collector comes around while your app is backgrounded.
If you are working with multiple threads, make sure that you are opening your database in a mode supporting a multithreaded environment.
If you open your sqlite db with a simple
SQLiteConnection conn = new SQLite.SQLiteConnection(path);
the database will open single-threaded.
Try this instead:
SQLiteConnection conn = new SQLite.SQLiteConnection(path, SQLiteOpenFlags.Create | SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.NoMutex);
If that doesn't help, please give a bit more insight into your code (how do you open the db, how do you insert/update data, etc...)

deployable database on CD

I have a Microsoft SQL database that is currently connected to a winforms C# application, it works fine on the single computer, but i would like it to be usable on a CD for any user.
I tried putting it in as a localDb but for some reason the database is duplicated and put into the bin folder, it causes multiple issues in recording data, for instances i save user ID 5 it saves in bin but never makes changes to the real database. Then next i go to create it, the user ID changes to 7 with user 6 not visible in either two databases (yes it is auto incremented by 1)
Any suggestions or best methods on making a database useable and readable via CD if the winform application is also on the CD
I have not tried this my self, but according to the documentation SQLite supports read-only databases.
If the file is read-only (due to permission bits or because it is located on read-only media like a CD-ROM) then SQLite opens the database for reading only. The entire SQL database is stored in a single file on the disk. But additional temporary files may be created during the execution of an SQL command in order to store the database rollback journal or temporary and intermediate results of a query.
see https://www.sqlite.org/c_interface.html
.NET SQLite providers are available here:
https://github.com/praeclarum/sqlite-net
http://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki

C# Sql Server Express not Saved Permanently

I am using Visual Studio C# 2008 and SQL Server Express. i got a question
i have the following data on my database
and then, i want to insert a new data
after that, i have to make sure that the entered data is saved on my database
it was there, the data was entered succesfully ! but then, when i take a look at my database table
the entered data was not saved permanently. i need to make the entered data saved permanently. how do i resolve this ?
thanks !
You are using the User Instances = true + AttachDbFileName "feature." When you use this "feature" each application you use will open a different copy of the original MDF file. So your C# app opens one copy, you insert a row, but this is never seen in the copy that is open in SSMS / Visual Studio or wherever else you might review the data.
To fix this, STOP USING THIS "FEATURE".
Create/attach your database to a proper instance of SQL Server, and point to it from your app and SSMS / Visual Studio by referencing the server and the logical database name, not the path to an MDF file.
You'll notice I called this a "feature" - in quotes - multiple times. This is because it is not a feature and has caused countless, countless users before you to become absolutely confused about why their inserts and updates "don't work"...

Load and save .sdf at runtime

I'm building an C#-application that uses an SQL Server Compact 4.0 database (.sdf) with Entity Framework for data. I want to be able to load/save-files from within this application so that the user can load a different database or backup the database to an USB eg.
I know you can create an sdf in code, but how can i load it at runtime (The connectionstring)?
My question is that this must be a common thing to do, what is the best way to go about it? is there any guides out there to do this?
or do you reccomend another way to go about my problem?
//ObjectiveCoder
You should use a SqlCeConnectionStringBuilder to create a connection string containing your file path.

Programmatically Repair SQLite Database

I have a need to try and repair a SQLite database from a .NET program if the database file gets corrupted. I have found several sites such as Fix SQLite and in the FAQ it describes that you can:
Depending how badly your database is corrupted, you may be able to recover some of the data by using the CLI to dump the schema and contents to a file and then recreate.
Does anyone know of a way to repair a SQLite database programmatically in .NET?
You might consider implementing your own strategy for database recovery. You could store backups of the SQLite file and then check that it is OK using:
PRAGMA integrity_check;
If errors are found then you can revert to a backup.
You are overlooking one important word: you can 'recover some data', this is not a repair!
If there is a sitatuation where a corrupted database could be repaired perfectly without user-interaction than it would not be corrupted in the first place and such an repair would have been a standard function of SQLite

Categories

Resources