在同一解决方案中为多个项目集中连接字符串[英] Centralize connection strings for multiple projects within the same solution

本文是小编为大家收集整理的关于在同一解决方案中为多个项目集中连接字符串的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我目前在解决方案中有三个项目,它们都有自己的app.config文件,带有相同的确切连接字符串.

是否有一种方法可以合并连接字符串/app.config文件,以便我只需要更改一个位置?

推荐答案

有几种方法可以做到:

  1. 如图所示,将常见的配置设置放入Machine.config中
  2. 将常见的配置设置放在中央文件中,并在每个项目的app.config中链接到在这里
  3. 将配置设置存储在注册表中

对我来说,我总是使用最后一个解决方案:) 祝你好运!

其他推荐答案

您可以在解决方案中的多个项目之间共享连接字符串,如下所示:

  1. 在解决方案文件夹下使用连接字符串创建ConnectionStrings.config文件,此文件应仅包含connectionStrings

  2. 的部分
  3. 在您的项目中,将此配置文件添加为链接(添加现有项目,添加为链接)

  4. 选择添加的文件并将其属性设置为Copy to Output Directory或Copy always或Copy if newer
  5. 在您的项目的App.config中,使用configSource属性指向链接ConnectionStrings.config文件: <connectionStrings configSource="ConnectionStrings.config" />

connectionstrings.config

<connectionStrings>
    <add name="myConnStr" connectionString="Data Source=(local); Initial Catalog=MyDB;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    ...
    <connectionStrings configSource="ConnectionStrings.config" />
    ...
</configuration>

读取更多详细信息... .

其他推荐答案

首先,看看这篇文章.它描述了如何在多个项目之间共享相同的app.config.

如何共享app.config?

第二,查看另一个帖子,该帖子描述了您如何让不同的app.config-files引用一个包含连接字符串的单个共享XML文件.

使用XML在app.config中包含或配置引用,以包含其他配置文件的设置

本文地址:https://www.itbaoku.cn/post/1938245.html

问题描述

I currently have three projects in my solution that all have their own App.config file with the same exact connection string.

Is there a way to consolidate the connections strings / app.config files so that I only need to make changes to one location?

推荐答案

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

For me, i always work with the last solution :) Good luck!

其他推荐答案

You can share the connection strings among multiple projects in a solution as follows:

  1. Create a ConnectionStrings.config file with your connection strings under a solution folder, this file should contain only the section connectionStrings

  2. In your projects, add this config file As a Link (add existing item, add as link)

  3. Select the added file and set its property Copy to Output Directory to Copy always or Copy if newer
  4. In the App.config of your projects, point to the linked ConnectionStrings.config file using the configSource attribute: <connectionStrings configSource="ConnectionStrings.config" />

ConnectionStrings.config

<connectionStrings>
    <add name="myConnStr" connectionString="Data Source=(local); Initial Catalog=MyDB;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    ...
    <connectionStrings configSource="ConnectionStrings.config" />
    ...
</configuration>

Read more details....

其他推荐答案

First, take a look at this post. It describes how you can share the same app.config between multiple projects.

How to Share App.config?

Second, take a look at one other post, which describes how you let different app.config-files have a reference to one single shared xml-file which contains the connection strings.

Use XML includes or config references in app.config to include other config files' settings