Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed .sqlite database in .exe c# application

Tags:

c#

sqlite

I want to embed the SQLite database into my C# windows form app (.exe) so I can give a very non-tech savvy client just the .exe file and nothing else. The database is read-write and I need to access its path for the DataSource like so:

 SQLconnect = new SQLiteConnection("DataSource=" + path + ";Version=3;Compress=True;");

I also tried saving the database to a folder on Desktop but SQLiteConnection.CreateFile(path) seems to fail because if I put the .exe file outside the bin/debug folder, .exe does not open at all (but it creates the necessary folder on Desktop).

But as long as it is in the bin/debug, it creates the database file and creates the tables and .exe works perfectly. There are no errors raised at any point in the application.

like image 629
Captain Otter Avatar asked Dec 08 '25 20:12

Captain Otter


1 Answers

To include the SQLite dlls you will have to modify the property of the reference and mark it as Embedded Resource. You will probably have to use ILMerge See this.

You cannot embed the database. So, you have the following two choices :

  1. You can create In-Memory/Temporary database.

    var connection = new SQLiteConnection("Data Source=:memory:");

  2. Create the database on the go. On your application startup, you can create and seed the database. See this.

like image 134
Harsh Avatar answered Dec 10 '25 10:12

Harsh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!