Embed .sqlite database in .exe c# application - c#

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.

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 :
You can create In-Memory/Temporary database.
var connection = new SQLiteConnection("Data Source=:memory:");
Create the database on the go. On your application startup, you can create and seed the database. See this.

Related

Sqlite database cannot be accessed after installation in WPF

I have created a WPF application and I have used a visual studio setup project inside the original solution to create an installer. This installer is created in a correct way and install the application on the system.
The problem is the fact that inside the application a sqlite database need to be created. Until the application is inside visual studio no problem are present, but once installed inside the main window an exception is raised:
System.UnauthorizedAccessException: Access to path 'C:\Program Files (x86)\User\TestApp\database.sqlite' denied
The code I use inside the app is the following:
if (!File.Exists("database.sqlite"))
{
SQLiteConnection.CreateFile("database.sqlite");
SQLiteConnection sQLiteConnection = new SQLiteConnection("Data Source=database.sqlite;Version=3;");
sQliteConnection.setPassword("1234");
sQliteConnection.Open();
}
Can you tell where the problem is? I need to place those elements in a special folder that is accessible after the installation? Or there is another method to ensure the app works after the installation on target machine?
Files in the Program Files or Program Files (x86), or any folder underneath, are typically set for read-only access unless your program is run elevated. This requires any write-access files or databases to be located elsewhere. Depending on what you're putting into it, ApplicationData or LocalApplicationData (from Environment.GetFolderPath) are the typical locations.

How to keep databases changes on access database on a portable executable created with Enigma Virtual Box

I am working on a desktop project with c# with Access database. Now I have finished my project and I need to have an executable file contains all the components and ddls and my access database of my project.
In other words, I want an executable file runs portable.
Fortunately I was able to create this portable executable file by Enigma Virtual Box and my project works fine on all windows without any installed.
But there is a problem that i could not solved it. when i inserted or updated or deleted any record in access database every thing is fine. but when i close my application and run again Everything is back to normal.
In other words, when I run the application previous database copied again in executable file.
In this case one can not help? Or there are other solutions for the portable executable file?
After any change of data base you must rebuild exe file, you can't save changes in exe file. So dont pack database.

How to add SQLite .db into C#.NET project

I just started to learn C#, and currently trying to create a simple program that read 1 row of data from SQLite database.
I've successfully created the program, but i still have to refer to the .db file with full address like :
SQLiteConnection("Data Source=C:/Users/../SQLiteStudio/dbs/mk_ii.db; Version=3;")
But this way, the .db won't be included when i create an installer. I want to make it something like when i add an image to my Solution Explorer, i can use short address like :
res/img/logo.png --i also have copy of .db file in res/ folder
So, is there any way to include the .db file to my project? I've tried to read current directory and using |DataDirectory|, but all of those referred to the .exe file's active folder, and didn't work when i debugging since when debugging the .exe file have different directory than the project directory.
For a note, i'm using VS Community 2015 and System.Data.SQLite. Also i've tried added the .db file to Solution Explorer, but i can't seem to access through it.
Include your database file in your project. Set the type to Content, that will make sure the file is copied to your output directory. Use relative path in connection string.

C# Express Edition + SQL Server 2008 R2 Express; Database file being recreated on run

I created a standard C# console application in 2010 Express, used the wizards to create a simple sql express db file which gets placed in the project dir, used the wizards to generate an entity data model based on the newly created db, wrote a few lines to test out entity.
Problem is each time I run the application it recreates the DB file in whatever dir the exe is in overwriting itself everytime and completly ignoring the original db file that sits in the project dir.
Does anyone know what I am missing here ?
Not entirely sure about this but try it out.
Make sure your connection string points to the database file in the project directory.
Select the file in Visual Studio and Choose 'Copy to Output Directory' ->> Do Not Copy.
Hope it works.
you must be using some way to find that file or some path parameter within the database connection that points to the database file.
Now either you can have that parameter be generated by code or make the directory specific and make the database file copy at a specific location from the original location so that the only that particular database is accessed using the application.
For doing that you can add another key in the application config file or at some other place so that the database your application is accessing is in the project directory itself at all times.

.NET including class library database (and other resources) with windows app deployment

I have a class library that attaches itself a tiny SQL Server database that resides in its Data Directory. When i'm using this class library with another windows application i see that once i compile my code, the database files get copied to the bin folder of my windows app project. However when i publish the windows app,install, and run it, i get the error 'An attempt to attach an auto-named database for file C:\Documents and Settings\User\Local Settings\Apps\2.0\Data..\DB.mdf failed.' Obviously this folder doesn't have the mdf files.
I guess this won't be a problem if i just add the database files to my windows application project. But surely there's a better way?
You could include an SQL script for creating the database into your "installation/run first time routine".
I guess that you've already stated that having a form of SQL Server is an installtion prerequisite.
For the data files I would recommend that you use a variable connection string for accessing your database. That way you can change the installation routine to include asking the user where they wish to have the data files installed and save that as part of your connection string to the app.config file.
Conversely you could also use the users selection of where to install the app to override the relative path stored for the database within your code (using the same connection string variable as mentioned above).

Categories