简体   繁体   English

保存数据库设置以供解决方案中的所有项目访问

[英]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). 我有4个项目(Windows服务,Windows表单,Web和共享代码)的解决方案。 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. 我希望所有4个应用程序都能够将数据库连接设置(服务器,数据库名称,凭据)写入和读取到所有应用程序都可以读取它的区域。

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. 当前,它正在保存在配置文件中,但是共享代码应用程序并不将其保存为自己的配置文件,而是分别保存在app.config和web.config文件中。

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. appSettings元素可以包含指向外部文件的文件属性。 I will change my web.config file to look like the following: 我将更改我的web.config文件,使其如下所示:

<?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. 接下来,我们可以创建外部文件“ externalSettings.config”,并添加一个appSettings部分,其中包含我们的连接信息和我们要使用的任何其他设置。

Reading connection string from external config file 从外部配置文件读取连接字符串

You would also need to save the settings you want in that particular file 您还需要将所需的设置保存在该特定文件中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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