views:

784

answers:

7

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?

+2  A: 

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

marc_s
The links seem like they are all for web apps, not winforms which is mainly what I'm working on.Your solutions seems the most straightforward for now.
muhan
A: 

you can make form asking the user to enter the connection parameters such as server name and database..., and store those parameters in Registry, files etc...

Wael Dalloul
+1  A: 

Where I work this is done by creating a folder called config that holds the various configurations.

source
 config
  MyProject
    environment1
    environment2
  MyProject2
    environment1
    environment2

When the build script is run, it grabs the correct config based on which environment you're doing the build for...

mezoid
Indeed, this is what we used to do. Now we use an application I've written to do the deployments automatically, and handle this as well. But post-build scripts are the way to do, in general.
Noon Silk
A: 

You should add an app.config file to your winform application. In this file store the connection string under the ConnectionStrings section.

In your code, when create a connection use that connection string using ConfigurationManager class to access config file.

So when you deploy your application, you have to chance the connection string once in config file and everything runs as expected!

michele
A: 

Hi,

You can defines your connections in settings, use dev connection in debug mode else production connection with #if directive.

//affect your prod connection here
    #if DEBUG
       //re affect your dev connection here;
    #endif

A link to explain : http://msdn.microsoft.com/fr-fr/library/4y6tbswk.aspx

Xstahef
that works until you have multiple environments to deploy to...at my work we have sit, uat, prod...
mezoid
A: 

Right-click on the web site in the Solution Explorer and select Add Web Deployment Project.

Whenever this new WDP project is built, it will replace all the configuration elements you specify. You can also have different versions depending on Debug or Release builds. It works for almost all of the configuration options, including the connection string. There's lots of documentation on how to configure it correctly on the net, just search for "Web Deployment Project".

This is definitely the new "default" way to do this until MS decides to make it more formal in some future version of .Net/Visual Studio, if they ever do.

Mufasa
A: 

disclaimer: I wrote what I'm talking about

We use a program I wrote, dashy, to handle this. Basically, it lets you configure your build so that it builds in a 'naked' or 'configuration-agnostic' way, and then you determine which environment you want to release to, and you do that (via a website running on your build server). There's more to it, but that's the basics.

Noon Silk