views:

278

answers:

4

I'm entering a parallel test and dev stage where I need to use one db for test and a different one for dev. How can I have the app choose which connection string to implement based on which physical folder it (the app) sits in?

I know there are SVN strategies to consider but this is small-scale enough to avoid 2 sperate code-bases. Would like to be able to publish the same VS project to either of my 2 directories without having to remind myself to change the connection string.

I'm running under IIS7 so perhaps it offers better control than conditionals in (and overrides) web.config. (or not)

thankx!

A: 

You could e.g. create a <connectionStrings> container that contains a connection string for each folder your app could be in:

<connectionStrings>
  <add name="Folder1" connectionString=".....(conn str. #1)...:" />
  <add name="Folder2" connectionString=".....(conn str. #2)...:" />
  ....
  <add name="Folder-n" connectionString=".....(conn str. #n)...:" />
</connectionStrings>

and then just pick the right one, depending on where your app starts up from.

Marc

marc_s
+2  A: 

A word of advice:

I wouldn't base your connection string on your published folder. Down the road, the folder might change, and folks may not be aware that that determines which connection string you're using.

Instead, control it with a setting in your web.config file. Just add a setting that allows you to switch between production and dev databases. In fact, you could simply test for the presence of a debug mode setting. If that setting is there, you're targeting the development database; otherwise, you're targeting production.

The nice thing about that solution is that it doesn't depend on where you deploy the site, and you can document the setting in the Web.config file.

Hope this helps.

Edit for Clarity: By "a debug mode setting" I mean a setting that determines which database you're targeting, dev/production. Not whether your application is running in Debug mode, since the Framework already provides a function that does that. Also, you wouldn't necessarily remove the setting, since you'd want to keep it for documentation purposes. Rather, you'd comment it out.

Mike Hofer
Hm... Basing the choise of connection string on the debug state seems even less obvious than basing it on the folder...
Guffa
If the setting is commented out in the web.config, requesting it from the framework will return null or throw an exception. But it'll still be documented. On the other hand, basing it on an arbitrary folder, which a developer can change at any point in time as the network configuration evolves, is far more volatile.
Mike Hofer
A: 

Get a unique string for the application, perhaps something like:

string folder = Regex.Match(Server.MapPath("~"), @"\(.+?)\$").Groups[0].Value;

Then use the string to get a value from the web.config:

ConnectionStringSetting connectionString = ConfigurationManager.ConnectionStrings["ConnectionString." + folder] ?? ConfigurationManager.ConnectionStrings["ConnectionString"];

In the web.config you can add several connection strings with the folder names appended for each dev site, and a default connection string with just the name and no folder.

Guffa
A: 

I usually put the connection strings into a separate config file and reference them from the main web.config using configSource:

In web.config:

<?xml version="1.0"?>
<configuration>
    <!-- connection strings are located in an external config 
     file to facilitate deployment in various environments -->
    <connectionStrings configSource="connections.config"></connectionStrings> 
</configuration>

In connections.config:

<?xml version="1.0"?>
<connectionStrings>
    <add name="ConnectionName" connectionString="[your connection string]"/>
</connectionStrings>

Once deployed, I usually exclude connections.config from future deployments (unless it should be changed, that is).

Fredrik Mörk