问题描述
我目前在解决方案中有三个项目,它们都有自己的app.config文件,带有相同的确切连接字符串.
是否有一种方法可以合并连接字符串/app.config文件,以便我只需要更改一个位置?
推荐答案
有几种方法可以做到:
- 如图所示,将常见的配置设置放入Machine.config中
- 将常见的配置设置放在中央文件中,并在每个项目的app.config中链接到在这里
- 将配置设置存储在注册表中
对我来说,我总是使用最后一个解决方案:) 祝你好运!
其他推荐答案
您可以在解决方案中的多个项目之间共享连接字符串,如下所示:
-
在解决方案文件夹下使用连接字符串创建ConnectionStrings.config文件,此文件应仅包含connectionStrings
的部分
-
在您的项目中,将此配置文件添加为链接(添加现有项目,添加为链接)
- 选择添加的文件并将其属性设置为Copy to Output Directory或Copy always或Copy if newer
- 在您的项目的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-files引用一个包含连接字符串的单个共享XML文件.
问题描述
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:
- Put common configuration settings in machine.config as shown here
- Put common configuration settings in a central file and link to that in each projects's app.config as shown here
- 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:
Create a ConnectionStrings.config file with your connection strings under a solution folder, this file should contain only the section connectionStrings
In your projects, add this config file As a Link (add existing item, add as link)
- Select the added file and set its property Copy to Output Directory to Copy always or Copy if newer
- 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>
其他推荐答案
First, take a look at this post. It describes how you can share the same app.config between multiple projects.
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