You can share the connection strings among multiple projects in a solution as follows:
Create a ConnectionStrings.config
file with your connection strings under a solution folder, this file should contain only the section connectionStrings
In your projects, add this config file As a Link (add existing item, add as link)
- Select the added file and set its property
Copy to Output Directory
to Copy always
or Copy if newer
- In the
App.config
of your projects, point to the linked ConnectionStrings.config
file using the configSource
attribute:
<connectionStrings configSource="ConnectionStrings.config" />
ConnectionStrings.config
<connectionStrings>
<add name="myConnStr" connectionString="Data Source=(local); Initial Catalog=MyDB;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
...
<connectionStrings configSource="ConnectionStrings.config" />
...
</configuration>
Read more details....