Centralize connection strings for multiple projects within the same solution

Currently, I have three projects in my solution that all have their own App.config file with the same connection string.

Is there a way to consolidate connection files / app.config files, so that I only need to make changes to one location?

+6
source share
3 answers

Here are some ways to do this:

  • Put the general configuration settings in machine.config as shown here
  • Put the general configuration settings in a central file and attach to them in each app.config project, as shown here
  • Save the configuration settings in the registry

For me, I always work with the latest solution :) Good luck!

+2
source

You can split connection strings between several projects in a solution as follows:

  • Create a ConnectionStrings.config file with connection strings in the solution folder, this file should contain only the connectionStrings section

  • In your projects, add this configuration file as a link (add an existing item, add as a link)

  • Select the added file and set its Copy to Output Directory property Copy to Output Directory Copy always or Copy if newer
  • In the App.config your projects, specify the associated 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> 

More details ...

+5
source

First, take a look at this post. It describes how you can share the same app.config file between multiple projects.

How to send App.config file?

Secondly, see another post that describes how you allow various app.config files to reference one common XML file containing connection strings.

Using XML includes configuration or configuration links in app.config to include other configuration file settings

+4
source

All Articles