C# – settings.settings connectionstring in development vs production

cvisual-studio-2008

I'm using Visual Studio 2008, developing a winforms app.

I have my database connection string stored in settings.settings.

My development and production environments require different database login credentials and right now I'm manually changing the connectionstring by hand before building for the 2 different environments.

Is there a better solution?

Best Answer

An eternal problem! :-)

Basically, right now, Microsoft doesn't really have a good answer for this.

What I would do is have both connection strings in my settings file, under two separate names, and then have a config setting in app.config which tells me which one to use:

MyDatabaseDEV = server=(local);database=mydatabase;-........
MyDatabasePROD = server=ProdServer;database=MyDatabase;........

and in app.config

<appSettings>    
   <add key="UseDatabase" value="MyDatabaseDEV" />
</appSettings>

This setting in app.config can be tweaked in your build process, e.g. by a "after build" batch file, or an MSBuild task or something, to be switched to "MyDAtabasePROD", when you do a production (release) build.

Microsoft promises us more and more flexible tools for .NET 4.0 / Visual Studio 2010, which should be out by the end of the year 2009. You should be able to create "config transformations" - check out these blog posts:

Marc

Related Topic