Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading an SQLite database from a byte stream in .NET

I'm trying to load an SQLite database that will be embedded in my executable as a resource. For those who are curious, it is because I use a small SQLite database to store configuration data, and I'd like to ship the default configuration embedded inside the executable (I hate having to carry files along with a program).

This would just be the default configuration. That is, I do not need to modify this configuration. It is static and cannot be changed after my program has been built.

I am using the System.Data.SQLite wrapper for SQLite.

I can get access to a byte stream like this:

using (var stream =
          Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) {
    // use of "stream" here.
}

However, I am at a loss with regards to creating an SQLiteConnection object from this stream.

The easy way out would be to simply spawn a new configuration file every time the program loads, and then tell SQLite to read from that file. This would probably work, but I would like to avoid that technique - I plan on reusing my solution to this problem to also include internationalization support (where each language is a [compressed] SQLite database and the default language is embedded in the executable - a user could add other languages my simply copying other language databases to the working directory of the program).

I'm not concerned with the memory cost of holding this database in memory. It will be very small (likely less than 50 KiB), so that does not bother me. Obviously if this was a larger database, this might be an issue.

Finally, I'm aware that this may be seen as a poor way to store a configuration. While I agree that a plaintext-based solution is nice for cases when the user enters all his/her settings manually, this would mostly be used for settings that the user does not explicitly define. For example, to store the positions of various docking windows in an application, or to store data about where internal resources might be found.

Thank you for your help.

like image 801
Ethan Avatar asked Jun 20 '11 23:06

Ethan


1 Answers

Seems like this functionality is not inmplemented in a System.Data.SQLite provider. You can't even save in-memory database to a file (i.e. in a binary format), only in form of an SQL dump.

So one of the solutions is to store SQL code for database creation in a resource. Then create in memory db each time application starts.

like image 102
Petr Abdulin Avatar answered Nov 09 '22 05:11

Petr Abdulin