IT Dept
IT Dept

Reputation: 3

How to configure a C# application database connection easily for development and production without changing the code and recompiling?

I'm creating a simple C# Windows Forms application that connects to a SQL Server database. We have a different server for developing and production, and now I'm changing the IP address of the server directly inside the code.

The application is going to be deployed, and I am looking for a way to configure things like database server, database name, etc, without changing the code and recompiling. This change will make easier to debug and deploy the application.

What is the recommended way to separate the configuration from the code?

Upvotes: 0

Views: 1182

Answers (3)

D'Arcy Rittich
D'Arcy Rittich

Reputation: 171351

Rather than using IP address, use hostnames. This way you can change the IP address without having to modify hostnames.

You can store connection info and other application settings in the application .config file.

Upvotes: 0

Jamiec
Jamiec

Reputation: 136074

What is the recommended way to separate the configuration from the code

With a configuration file of course. http://www.dreamincode.net/forums/topic/45321-grabbing-connectionstring-from-appconfig/

Upvotes: 1

Wallace Breza
Wallace Breza

Reputation: 4948

Add an app.config file to your application and store your database connection settings in there.

This will allow you to simply modify the config file without having to recompile the whole application.

Upvotes: 1

Related Questions