Search code examples
sql-serverwpfdatabase-connectionsetup-deploymentlocal-database

WPF C# : How to set Dynamic Connection String of SQL Server local database


I want to deploy my WPF project but at the client machine it throws an exception that connection is not able to open.

I use a hardcoded string I don't know how to set dynamic connection string which changes at every computer. Someone please guide me with deployment of a WPF project with local SQL Server database or how to set connection string dynamically.

What I have done:

  1. Deploy and export database with .EXE
  2. Deploy without database and with SQL script so on the client machine it creates database before starting setup

This is the connection string which I am using:

<add name="connect"
     connectionString="Data Source=(LocalDB)\FoodManagmentUpdate;AttachDbFilename=C:\Program Files (x86)\SolBuz\Shreeji Snacks\FoodManagmentUpdate.mdf;Integrated Security=True"
     providerName="System.Data.SqlClient" />

I already tried with :

<add name="connect"
     connectionString="Data Source=(LocalDB)\FoodManagmentUpdate;AttachDbFilename=|Data Dictionary|FoodManagmentUpdate.mdf;Integrated Security=True"
     providerName="System.Data.SqlClient" />

I installed SqlLocalDb on my client's machine, but still it won't work. And FoodManagmentUpdate is the object of SqlLocalDb which I also created on the client computer.


Solution

  • I Changed my AttachDbFileName=|DataDirectory|\Databasename.mdf and now it works but i have to copy whole folder 'bin' which resides in your solution folder and that bin folder must contain your database in it. This way i get my solution.