Settings.settings connectionstring in development versus production

I am using Visual Studio 2008, developing a winforms application.

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

My development and production environment requires different credentials to enter the database, and now I manually change the connection string manually before building for two different environments.

Is there a better solution?

+4
source share
6 answers

The eternal problem !:-)

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

What I would do is both the connection strings in my settings file under two separate names, and the configuration settings 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 parameter in app.config can be changed in your build process, for example. using the “after build” batch file or the MSBuild task or something else to switch to “MyDAtabasePROD” when you create the build (release).

Microsoft promises we are increasingly flexible tools for .NET 4.0 / Visual Studio 2010, which should be released by the end of 2009. You should be able to create "configuration conversions" - check out these blog posts

Mark

+2
source

Where I work, this is done by creating a folder called config, which contains various configurations.

 source config MyProject environment1 environment2 MyProject2 environment1 environment2 

When the script build is in progress, it captures the correct configuration based on what environment you are building the build for ...

+1
source

you can make a form asking you to enter connection parameters, such as server name and database ... and save these parameters in the registry, files, etc.

0
source

You must add the app.config file to your winform application. This file stores connection strings in the ConnectionStrings section.

In your code, when creating a connection, use this connection string using the ConfigurationManager class to access the configuration file.

Therefore, when you deploy your application, you must accidentally enter a connection string in the configuration file, and everything will work as expected!

0
source

You can define your connections in the settings, use the connection to the connection in debug mode of another production connection with the #if directive.

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

Link for explanation: http://msdn.microsoft.com/fr-fr/library/4y6tbswk.aspx

0
source

Right-click the website in Solution Explorer and select "Add Network Deployment Project."

Whenever this new WDP project is created, it will replace all the configuration items you specify. You may also have different versions depending on the build of Debug or Release. It works for almost all configuration options, including the connection string. There is a lot of documentation on how to properly configure it on the network, just search for "Project Web Deployment Project".

This is definitely a new “default” way for this, until MS decides to make it more formal in some future version of .Net / Visual Studio, if any.

0
source

All Articles