Today I use SQLite and send a database-file with the project.
However I want the database to be created when the user first starts the software.
Is there a way to copy the code needed to create a database based on the existing database? The problem is that when a user downloads a new version he might be tricked into copying over his last database and lose the data. I'd like a nice way to check the version of the database and modify it if I need new columns or tables etc. Or, if it does not exist at all, create a new database?
I know I can probably make create the code to make the database from the beginning but I want it to be based on the existing database I have created by a gui.
Answered. Final code:
//Copy the database from the resource
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("resourcename"))
{
if (stream == null)
throw new NullReferenceException("Stream is null. Cannot find the database in the resources");
FileStream writer = new FileStream(Constants.SqLiteFile, FileMode.Create);
int Length = 256;
Byte[] buffer = new Byte[Length];
int bytesRead = stream.Read(buffer, 0, Length);
// write the required bytes
while (bytesRead > 0)
{
writer.Write(buffer, 0, bytesRead);
bytesRead = stream.Read(buffer, 0, Length);
}
stream.Close();
writer.Close();
}