views:

461

answers:

3

OK I have two web projects WebProject1 & WebProject2. Both require database connectivity so this is all in a C#.NET project called Common.

Now my question is currently the connection string for both is the same and at the moment it's hard coded into the DB class (In the Common project), but I want to move it out to a config file.

I would really like to have a config file in the Common project with the connection string in which both web projects then use. Is this possible, and if so how?

A: 

to share connection string between multiple projects, you need to put in machine.config.

Muhammad Akhtar
The connectionstring should not be stored in machine.config. Any web-application on the machine will gain access to it.
this. __curious_geek
@s_ruchit. If you use Oracle and TNS names it effectively works in a similar way to this anyway. If you control the environment this wouldn't be an issue. All the connections strings I use to SQL Server use integrated security with permissions granted to specific service accounts.
RichardOD
@S_ruchit. If you using SQL Server security instead of integrated windows, then I can see the validity of your point. If I am doing anything that requires a password in a config file I always encrypt it.
RichardOD
+2  A: 

Hi,

There's a few ways you could do it:

  1. put common configuration settings in machine.config as shown here
  2. put common configuration settings in a central file and link to that in each projects's app.config as shown here
  3. store the configuration settings in the registry
AdaTheDev
Your application may not have rights to read/write from any location in production server except the application directory. It may also not have rights to do registry R/W.
this. __curious_geek
I don't like option 3. Registry settings are a bit of a backwards step and they are awkward to put in source control.
RichardOD
The idea of linking to the configSource for connection strings seems a rather good idea. All I have to do then is copy the Common.config file to the target bin directory. I will give it ago see how well it works, not too sure about hard-coding the path to the config file in the bin dir though.
Adam Price
+1  A: 

Here's a way to do this.

Seperate your DataLayer that interacts with the database by making it a class library project. The class library project will produce an assembly which you can further refer to any project you want. In this project add 'app.config' in the project-root and store your connection string into the app.config. Your data-access classes in the projects can then refer to the connection string in the app.config. When you compile and deploy your data-access project into an assembly the app.config is embeded into the assembly. Now you can add this assembly as reference to as many as projects you want to share the connection and data-access.

this. __curious_geek
then how we will change connection string on deployment?
Muhammad Akhtar
You can change the connection string in app.config before deployment.
this. __curious_geek