This project is read-only.

How to Manage Database Dependencies

- J.D. Meier, Jason Taylor, Alex Mackman, Prashant Bansode

The following procedure explains how to store and then reference a database connection string within a user configuration file.

To use a user configuration file to store database connection strings

Add a configSource="user.config" attribute to the <connectionStrings> element of your main application configuration file as follows:

   <connectionStrings configSource=”user.config”/>

To override the main application configuration file, create a User.config file (located in the same folder as the application configuration file), and then add a similar <connectionStrings> entry to the file.
Notice that the following connection string references a local database:

      <add name="DBConnStr"
     connectionString="server=localhost;Integrated Security=SSPI;database=Accounts"/>

Within your project, use the following code to obtain the connection string from the user configuration file.
This code uses the static ConnectionStrings property of the System.Configuration.ConfigurationManager class. In the Win Form application, you must add a reference to System.Configuration.dll explicitly.

using System.Configuration;
private string GetDBaseConnectionString()
  return ConfigurationManager.ConnectionStrings["DBConnStr"].ConnectionString;

Ensure that the User.config file is deployed along with the application code. To do so, in Solution Explorer, right-click the User.config file, click Properties, and then in the Properties pane, set the Copy To Output Directory property to Copy if newer.

Do not add the User.config file to source control. By not doing so, each developer (and the test team) can explicitly specify the connection string through his or her own User.config file. Source control might contain other User.config files; for example, for testing and production. These files should be managed by the users responsible for managing the testing and production environments. These test and production User.config files should not be stored as part of the database projects but should be stored in different areas of the source control system.

In source control you should have a User.config file for each of the environments that you use, such as production and test. These configuration files should specify the connection string for the database. The User.config file must be present for the configuration system to work.

Tip: By default, the user configuration file is automatically added to source control when you add the solution. To prevent this, when you first check in the files, clear the User.config file check box. You can then right-click the file in Solution Explorer and select Undo Pending Changes to ensure that the file never comes under source control.

It is important to understand that the User.config file must be present if you are using this mechanism. Some team member will need to be responsible for ensuring that the environment is correct when creating builds for production releases and for any test environments. As part of this build step, the appropriate User.confg file will need to be retrieved from the source control system and copied into the correct location in order for MSBuild to be able to find it.

Additional Resources

Last edited Aug 16, 2007 at 7:33 PM by prashantbansode, version 5


No comments yet.