Packaging a C# application with a database


I have packed a WPF C# application that is using a database.

However, when I install the packaged application it is using the database I created at the start through visual studio (.mdf) rather than creating another one.

The whole purpose of packaging the application was so that I could share it on other computers, so I was install it I need it to create a new database for that instance of the application.

Is there a way to accomplish this? Is there a property I need to set to say that the database is a local instance? Or force the package to create a new one on installation?

Any help would be much appreciated.



Just been in the middle of doing this myself, one thing I would suggest is using SQL Compact Edition makes the process a lot easier, but if it's too late in the process at the moment then stick with the current database, However I do not know if the below method works with an MDF file.

How are you deploying the application on the client machine? does the users machine have sql installed?

The first thing you need to do is go to the properties of your MDF and ensure copy to output directory is set to copy if newer and build action is set to content.

If you're using a setup project to install the application then you need to make sure in project, application properties, publish, application files that you're mdf is in the list and is set to Data File auto, required and include.

and right click on the "Application Folder" in the installers file system, then add, project output, file contents

Finally change the connection string in your code so that it reads

using ( SqlCeConnection sqlCon = new SqlCeConnection( @"Data Source=|DataDirectory|\App.sdf;Persist Security Info=False;" )

or if you're not using compact Edition

using ( SqlConnection sqlCon = new SqlConnection( @"Data Source=|DataDirectory|\App.mdf;Persist Security Info=False;" )

^using "DataDirectory" means that you don't care where the user puts the application, you will always get the right file

before you run and change your database to compact, it doesnt support a lot of things a proper sql database will.

After all this, you may still encounter errors with permissions and because its quite long winded I might have missed out a step so just comment if you need more?

<em>EDIT</em> If the user does not have either sql server or sql Compact, your easiest solution would be to require them to install SQL CE or there is a way of using DLLs that do the same thing but I couldn't get that to work

the link for that is here <a href="http://msdn.microsoft.com/en-us/library/aa983326.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/aa983326.aspx</a>


Make sure your db is deployed client side and change the connection string in the application to point to it.


  • Spring - how to override internal config file with external file
  • Write to a Google spreadsheet from JavaScript using the Public API access (no OAuth)
  • JUnit: how to access Spring configuration as Spring has intended?
  • How can I set up Lazy Loading with ZF3 (no ServiceLocator pattern from anywhere)
  • .NET check if two IEnumerable have the same elements [duplicate]
  • Deploy Spring Boot to Wildfly 10
  • How to build a static library from Ada source that's callable from C++ code?
  • How to get submap of LinkedHashMap by element index?
  • Working with search bar on iOS 9 [closed]
  • C Standard Library Functions vs. System Calls. Which is `open()`?
  • Calling SaveAsync method sends email to attendees for existing appointment?
  • How to sort the union datastream of flink without watermark
  • C# Spell checker Problem
  • Why doesn't the tray icon context menu work for my RemoteApp?
  • JavaFX resize children relative to parent
  • Should the client have the same keystore as the server?
  • Spring Mvc submit/delete checked (selected) records from table
  • How to read contents of a directory recursively in Linux Kernel?
  • Preloading webView doesn't work - trying to reduce loading time
  • Creating and managing two independent random number sequences
  • Detect when MathJax has finished loading in UIWebView
  • Bazel failed to include a external static library .a
  • Could not resolve all files for configuration ':react-native-vector-icons:classpath'
  • didSelectItemAtIndexPath of UICollectionView inside of a UIScrollView is not getting called
  • How to convert days into months using datetime in Python3?
  • Implementation of timeout in LDAP
  • Get name of days between two date in ios?
  • Getting Microsoft Calibri font on Amazon EC2 ubuntu
  • Synchronous Calls with jqGrid?
  • Does hibernate load two seprate copies of same instance if they are loaded twice from database?
  • How to use Typescript with libraries like Ampersand.js that parse configs to build prototypes
  • Excel File upload in asp.net using SqlBulkCopy
  • Separating definition/instantiation of template classes without 'extern'
  • Create an average of multiple excel chart without the data source
  • How to make 100% div height between header and footer?
  • DataTables move rows between tables
  • how to run ejabberd with Erlang on Heroku?
  • How to use Streams api peek() function and make it work?
  • `$http:badreq Bad Request Configuration` - from angular post method, what is wrong here?
  • Jersey serializes character value to ASCII equivalent numeric string