简体   繁体   中英

Save Database Settings to be accessible by all projects in solution

I've got a solution with 4 projects (Windows Service, Windows forms, Web and Shared code). I would like all 4 apps to be able to write and read the database connection settings (server, db name, credentials) to an area where all apps can read it.

Currently it is being saved in the config file, but the Shared code app does not save it it's own config file, but in the app.config and web.config files respectively.

Is there a way that I can save it in a general space to be accessible by all?

Create another configuration file in the parent of all these apps in which you'll store the connection string.

Then customize the loading of the config file to take this file into account.

The appSettings element can contain a file attribute that points to an external file. I will change my web.config file to look like the following:

<?xml version="1.0"?>
<configuration>
    <appSettings/>
    <connectionStrings/>
    <system.web>
        <compilation debug="false" strict="false" explicit="true" />
    </system.web>
    <appSettings file="../externalSettings.config"/>
</configuration>

Next, we can create the external file "externalSettings.config" and add an appSettings section with our connection information and any other settings that we want to use.

Reading connection string from external config file

You would also need to save the settings you want in that particular file

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