views:

91

answers:

5

If I am to have connectionstrings read from a config file in my application and I need to be able to change the connectionstrings as the application is moved from dev to uat to prod should I be using settings files at all as there the connection strings are compiled into the assembly which I will be moving from environment to environment making the initial connection strings obsolete?

What is the best practice here?

Edit:

I am asking in general as I have both webservices as well as smartclient apps.

+2  A: 

You want to use the App.config xml file for desktop apps or web.config if you are doing web development. See "ConnectionStrings"

Brian Genisio
+1  A: 

I usually put the connection strings into an external config file, that I exclude from deployment: In web.config:

<connectionStrings configSource="connections.config"></connectionStrings>

connections.config:

<?xml version="1.0"?>
<connectionStrings>
    <add name="connectionName" connectionString="[connection string goes here]"/>
</connectionStrings>

[update]This assumes that you are building a web site; otherwise it is of course "App.config", not "web.config"

Fredrik Mörk
that's an interesting way to do it. I have my connection strings on my dev/production boxes in the machine.config encrypted. Works great for me ;) I guess you ship off a template for the IT guys to setup with that
Chad Grant
A: 

It appears you don't have the dot('.') button on your keyboard. Visit the shop to buy a new one.

I'm not sure if I understand your problem. Use config/settings file next to your application executable. Read that file for the connection string. If the file is missing create a new config file with a default url?

PoweRoy
A: 

Compiling configuration settings makes me cringe.

But if you're worried about your config being viewed by bad people.

Encrypt it: http://www.4guysfromrolla.com/articles/021506-1.aspx

Keep in mind that the encryption is based on the Machine Key, so that wouldn't work out so well deploying desktop apps.

Chad Grant
+1  A: 

We use the connection strings section of the .config file, and have string names like:

AppDataInDev and AppData

Also in .config file we have a key called InDev under application settings

In our datalayer we check the value of InDev. If true, the datalayer is initialized using the AppDataInDev otherwise, it uses AppData.

This makes management of the transition to prod easy. When publishing to prod, switch value of InDev to False and you are done. (If ASP.Net app, do this at the same time you switch CompilationDebug to False also.)

ChrisC_46259
I understand how the indev switch facilitates deployment but this way you have all your connection strings in the config files.Also having dev/prod switches in the code sounds dangerous as its a very easily forgettable to flip the switch. I would rather the application throws exceptions than to connect with some default connection string.
Fadeproof