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

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

Related

Embed .sqlite database in .exe c# application

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.

Where can I install a database in a client PC and use its path as a connection string?

I have created a software complaint tracking system in C#, using Access Databases.
Basically, it's done, but I am questioning where to install it on my client's PC?
I tried to install it in the DataDirectory, but I'm getting an error with that:
Operation must use updateable query in application.
My Connection String is Datasource=|DataDirectory|\\convert.accdb
I created the setup project by installshield
How can I fix this problem?
Since you have a ConnectionString with |DataDirectory|, it's very likely that the database file is in the programs folder and due to security permissions the user can't write to the file.
So you should modify the location where the database file is installed (APPDATA if it's per user, see below).
The connection can stay the same if you add the following code to tell your application that the placeholder |DataDirectory| should be replaced with the APPDATA-folder
AppDomain.CurrentDomain.SetData("DataDirectory", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
|DataDirectory|
|DataDirectory| (enclosed in pipe symbols) is a substitution string that indicates the path to the database. It eliminates the need to hard-code the full path which leads to several problems as the full path to the database could be serialized in different places. DataDirectory also makes it easy to share a project and also to deploy an application.
To set the DataDirectory property, call the AppDomain.SetData method. If you do not set the DataDirectory property, the following default rules will be applied to access the database folder:
For applications that are put in a folder on the user's computer, the database folder uses the application folder.
For applications that are running under ClickOnce, the database folder uses the specific data folder that is created.
Source: SQL Server Forum (Emphasis added by me)
Locations for installation
10.1 Your app must be installed in the Program Files folder by default
For native 32-bit and 64-bit apps in %ProgramFiles%, and %ProgramFiles(x86)% for 32-bit apps running on x64. User data or app data must never be stored in this location because of the security permissions configured for this folder.
10.3 Your app data, which must be shared among users on the computer, should be stored within ProgramData
10.4 Your app’s data that is exclusive to a specific user and that is not to be shared with other users of the computer, must be stored in Users\<username>\AppData
Source: Certification requirements for Windows Desktop Apps
(Emphasis added by me)

My application cannot update Access database after deployment to "C:\Program Files\..."

I have deployed my application to be ready for use by another user (another computer), but when I try to add the data to the database I get the error
Operation must use an updatable query
The error is like this (This is when I already deployed my program and run under application, not under Visual Studio):
But it works perfectly under visual studio, the image like this (note that, the error on the image above appear once I click the submit button, it supposed to stored in the database, and display it on the datagridview as like image below):
And also I got another problem, the delete function is not running, the error on the add and delete appear once I deployed my program, but I will post that on another thread.
How do I solve this?
When a Visual Studio application is under development it resides in a folder to which the developer has read/write access. This is obviously necessary since the developer needs to be able to edit the source code files. If you place a database file "in with the code" then the application can update the database file because it is in a "writable" location.
However, if on deployment the database file stays "with the code" and the installer puts the files (i.e., the executable file and the database file) into %ProgramFiles% on the target machine (for example, C:\Program Files\MyApplication) then the average user will not have write access to that location. Files in %ProgramFiles% are normally restricted to read-only to protect the system from malware.
Some people will try and configure the installer to grant write access to normal users for some file(s) or folder(s) under %ProgramFiles% but that is a Bad Idea™. The installer should really copy the database file to a location that is normally read/write for the intended user(s): either %USERPROFILE% (for a specific user), or %PUBLIC% (for all users).

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.

error deploying C# project with sql database : unable to open physical file

I am deploying C# windows application with an sql server database
This is the error I get when I deploy:
It says your database file is missing. Make sure you deploy the database file with your project.
See MSDN for more information. If you have your MDB file as part of your project, make sure that its properties are set to be deployed, specifically the Build Action (set to Content) and Copy to Output Directory(set to Copy if newer) properties. Your requirements may vary though.

Categories