WPF Desktop app how to include a sqlite DB - c#

I am developing a WPF desktop application that uses a SQLite DB and this is in my App_data folder which is fine while a run it in debug but how do i include a DB file when i build my application and run it on another computer? do i need to build an installer for my app to create a copy of the DB in a location on the users machine that has read/write access?
Thanks

You could create the database in your initialization routines if the database doesn't exist. You can include the location and other settings in your app config. Check this link out: Create SQLLite Database and Table

It sounds like you want to have your SQLite database included in your project output including all the records that were added (i.e. you do NOT want to deploy an empty copy of the database, but rather the same data that you were working with during development).
To accomplish this, add the SQLite database to your project and set the build action to "copy to output location". In your config file, you can then set up the connection string to look for the file in the application directory.
Now, every time you build the project, a fresh copy of the database will be placed in the output directory.

Related

publish a project with local database

I created a Windows form applicatie with a local database (.mdf) to store and retrieve data from.
the database where I connect to is:
C:\ProgramData\project\Database.mdf
when I publish my project and place my database file in that folder on a other pc and try to run it I get the error unable to locate a local database runtime installation
my connection string is:
conn.ConnectionString = #"Data Source=(LocalDB)\v11.0;AttachDbFilename=""C:\ProgramData\project\Database.mdf"";Integrated Security=True";
so could somebody help me with this problem?
because everything runs fine on my own pc
Did you include the database as "Application File"? If not do the following (at least this is how I am doing it):
Project -> Properties -> Publish -> Application Files
Here set the values for your .mdf and the xx_log.ldf as follows:
Now still in the Publish tab go on Prerequisites. Here you have to check the following depending on what database you are using.
This will download SQL Server Express for the client who is installing your application.
You will also have to change the connection string to a generic path. I suppose the database lies somewhere inside your project folder /bin I guess, not sure anymore. So adjust your connection string to something like:
Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True
I therfor recommend using a resource file or app.config
But basically i think your problem is that the pc you are installing on does not have SQL Server installed. So just follow the steps above in Prerequisites. The other steps will enable you to deploy the database to the project folder without moving it to a certain folder manually.
I hope this helps.

Data lost from .mdf file as I exit application

Recently I work on SQL Server 2008 R2 database. I create database and attach .mdf file of same database in my application with some default data in it . Run application default data coming properly. Now I insert, update some data in my application and its works fine. But as I exit application and again run application lastly added and updated data get lost but default data coming proper as earlier. Please help. Why new
As mention by #Henk , #Microtechie , I scan my project folder and found there are 3 copies of .mdf file are there, 1st in project folder where code project([ProjectFolder ]) resides 2nd in [ProjectFolder]/bin/debug folder and 3rd in [ProjectFolder]/bin/release folder and suddenly solution to my que trigger in my mind. Problem not in multiple .mdf files in project folder, as I every time ‘Clean’ and ‘Build’ my solution new copy of .mdf file from [ProjectFolder] get copied into [ProjectFolder]/bin/debug folder, result in override of last .mdf file in same folder. Hence every time I build and run application only default data coming and last added and updated data get lost.
Thanku all for your replies and precise answer..!
What kind of object do you load your .mdf file data into? It is likely that you need to save the changes in that object before closing the application.

Automatically install access database when creating setup file in c#

I Have created a c# application which is using access database.
I have deploy the application successfully.
what I want is install the access database automatically in other users computer automatically.
I have tried to change the app.config files connection string and no use..:(
help me
Not sure what you mean, but if your database is part of your solution you can copy it to the output directory:
You have to be more specific. Did you install the application using installer? Copying and pasting?
Maybe these would help:
StackOverflow - "How to install visual studio 2010 setup project with MS Access database on a computer which is not having Office 2010 installed?"
CodeProject - Deploy your Application and Database
MSDN Forums - "How to attach sql database file to creating .exe setup in c#"
Copy the access db to a folder in programdata with the setup. Connect to this db in your code. You don't need a connectionstring in app.config if the user is not allowed to specify the db path.
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
Your setup should be able to deploy to a programdata folder.
If you want to create .exe Setup Project of your Dot Net Application which is using MS Access DATA BASES. then simply follow this steps. Please read this one for a moment before to proceed these steps.Connect with MS Access Data Base in Dot net.
When you are going to create the exe file then your data base connection string should be like this(means there should be no directory in #"Data source, Just direct put the name of your data base, because in .exe file when you add your data base file, then it is in the same directory.
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
conn.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;" +
#"Data source=MyDataBase.accdb";
Right click on your solution file. (like in Solution Explorer "Solution 'MyProject' ")
Select Add -->New Project -->Other Project Types
Select "Setup"
A New project will be added named setup (with a new tab added to
editor as File System )
Now right click on application folder -->select add -->select folder
new folder will be added under application folder * Now Right click
on newly added folder -->select add -->select file * Add File dialog
will be popup
Select your Db file (means from a "Browse" Button there go and select your Access Data Base )
Select Setup project and build.. Now your
installer will have your DB.
You can also install it from there.

Linq To Sql update mdf file using clickonce

I have a application that use Linq2sql and have a database (mdf file). I using clickonce to release the application. I need to make changes in the database structure, How can i do this and update the mdf files using clickonce without losing the data at the files?
If you place your mdf in the data directory then you can have access to that previous-version-mdf-file when the newly installed version is run for the first time, since old data files are accessible in a PRE directory.
So at that moment you can copy the data from the old mdf to the new mdf.
To give you the full picture, are data files are renewed when a new installation is done. Its up to you to copy any old data.
Link which explain the first run of a clickonce installation:
How do I detect the first time a ClickOnce-deployed application has been run?
Link which mentions the PRE data directory to access files from the previous version.
clickonce - does writing/reading to the Data Directory required Admin rights?

main considerations while publishing application using SQL server Database

I want to ask about the main considerations we have to put in mind when publishing application using SQLServer database?
I ask this question because I faced a problem when I developed an application using SQL server Database in C# and used it in another machine. The problem is an exception happen in launching the application.
This is the exception message:
<b>"The ConnectionString proberty has not been initialized"</b>. The ConnectionString is in App.config and assing while form loading.
This is the ConnectionString:
<b>"Data Source=.\SQLEXPRESS;Initial Catalog=test;Integrated Security=True;Pooling=False"</b> and it is in this file: appName.exe.config
The application is Desktop application and it's not need for installation just click .exe and it should connect to the existing SQLServer database with the ConncetionString in the appName.exe.config file.
The best way to deploy any type of application will be creating a setup.exe or .msi file.
Since this will ensure the important files which will copy to the target machine. It also provides a well structured file hierarchy along custom action which we can program each and every steps of installation.
It is always good to keep the connection properties in a config file, which helps to change the configuration settings without building the project. The exe will reads data from the config file, so if we changed the values then also it will works fine. While creating the setup project add the configuration settings and deploy or it is also good to have a common setup msi and different config files.
For example the database may be different for servers like DEV,QA,Staging etc. For each sever the msi will be same but the connection string will be different. So there is also a way to create self extracting files which will update the config files . For QA,DEV,Staging etc different self extracting files will be there. by running those files, it is possible to update the server details and authentication details. By providing msi and extracting files, users can install the application very easily

Categories

Resources