简体   繁体   中英

Share a Connection string between multiple web projects

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM