如何从另一个项目访问连接字符串

时间:2021-03-19 16:38:02

I got two project in my solution in Visual Studio 2010.
Project 1 contains an app.config with a ConnectionString.

我在Visual Studio 2010的解决方案中有两个项目。项目1包含一个带有ConnectionString的app.config。

How can I access that ConnectionString from Project 2?

如何从Project 2访问ConnectionString?

Since they are both using the same ConnectionString, I would prefer to only have one instance in my solution.

由于它们都使用相同的ConnectionString,我宁愿在我的解决方案中只有一个实例。

5 个解决方案

#1


5  

You can add the files as a link to the file to one of your projects.

您可以将文件作为文件的链接添加到您的某个项目中。

Use Add an Existing Item to the project you want to add the file to - the Open button has a small down arrow, if you click that you can select Add as Link.

使用“将现有项目添加到要添加文件的项目” - “打开”按钮有一个小的向下箭头,如果单击该项,则可以选择“添加为链接”。

This will add the file as a link from the original location, meaning you only have one physical file.

这会将文件添加为原始位置的链接,这意味着您只有一个物理文件。

#2


3  

Another idea is to use an IoC container, like Ninject, injecting the connection-string into the constructor of the class you need it in. This is not unlike the factory pattern.

另一个想法是使用像Ninject这样的IoC容器,将连接字符串注入到您需要它的类的构造函数中。这与工厂模式不同。

Note: you don't need to be using Entity Framework to use Ninject.

注意:您不需要使用Entity Framework来使用Ninject。

See this post: Ninject - binding constructors with arguments / Entity Framework connection string

看到这篇文章:Ninject - 带参数/实体框架连接字符串的绑定构造函数

#3


1  

Beside of the file linking suggested in the answer by Oded, you may consider refactoring your application to use a commom data access assembly that contains a DatabaseConnectionFactory class or the like. This assembly would contain the connection string in its settings

除了Oded的答案中建议的文件链接之外,您可以考虑重构您的应用程序以使用包含DatabaseConnectionFactory类等的commom数据访问程序集。此程序集将在其设置中包含连接字符串

#4


0  

If there is only specific section you'd like to share (connectionStrings in your case) then linking wouldn't work for you. Instead you could do something like this:

如果您只想分享特定部分(在您的情况下为connectionStrings),那么链接将不适合您。相反,你可以做这样的事情:

The solution is to store the connection strings on the web.config of the parent web app. 
Note that the web site root is also an app, so you can store a web.config in there (i.e. c:\inetpub\wwwroot\web.config) which will be inherited by all apps under it.
c:\inetpub\wwwroot\web.config -> common configuration. 
c:\inetpub\wwwroot\app1\web.config -> configuration for app1
c:\inetpub\wwwroot\app2\web.config -> configuration for app2. 

In the case the default web site root is off limits, you can create a root app to contain all other apps and store the common configuration there. 
c:\inetpub\wwwroot\myrootapp\web.config-> common configuration. c:\inetpub\wwwroot\myrootapp\app1\web.config -> configuration for app1 c:\inetpub\wwwroot\myrootapp\app2\web.config -> configuration for app2.

#5


-1  

If your Project 2 has a reference of Project 1 then Project1 may have a class with a ConnectionString property exposed.

如果您的项目2具有项目1的引用,则Project1可能具有暴露ConnectionString属性的类。

By the way, the "class" may read the connection string from the app.config

顺便说一句,“类”可以从app.config读取连接字符串

#1


5  

You can add the files as a link to the file to one of your projects.

您可以将文件作为文件的链接添加到您的某个项目中。

Use Add an Existing Item to the project you want to add the file to - the Open button has a small down arrow, if you click that you can select Add as Link.

使用“将现有项目添加到要添加文件的项目” - “打开”按钮有一个小的向下箭头,如果单击该项,则可以选择“添加为链接”。

This will add the file as a link from the original location, meaning you only have one physical file.

这会将文件添加为原始位置的链接,这意味着您只有一个物理文件。

#2


3  

Another idea is to use an IoC container, like Ninject, injecting the connection-string into the constructor of the class you need it in. This is not unlike the factory pattern.

另一个想法是使用像Ninject这样的IoC容器,将连接字符串注入到您需要它的类的构造函数中。这与工厂模式不同。

Note: you don't need to be using Entity Framework to use Ninject.

注意:您不需要使用Entity Framework来使用Ninject。

See this post: Ninject - binding constructors with arguments / Entity Framework connection string

看到这篇文章:Ninject - 带参数/实体框架连接字符串的绑定构造函数

#3


1  

Beside of the file linking suggested in the answer by Oded, you may consider refactoring your application to use a commom data access assembly that contains a DatabaseConnectionFactory class or the like. This assembly would contain the connection string in its settings

除了Oded的答案中建议的文件链接之外,您可以考虑重构您的应用程序以使用包含DatabaseConnectionFactory类等的commom数据访问程序集。此程序集将在其设置中包含连接字符串

#4


0  

If there is only specific section you'd like to share (connectionStrings in your case) then linking wouldn't work for you. Instead you could do something like this:

如果您只想分享特定部分(在您的情况下为connectionStrings),那么链接将不适合您。相反,你可以做这样的事情:

The solution is to store the connection strings on the web.config of the parent web app. 
Note that the web site root is also an app, so you can store a web.config in there (i.e. c:\inetpub\wwwroot\web.config) which will be inherited by all apps under it.
c:\inetpub\wwwroot\web.config -> common configuration. 
c:\inetpub\wwwroot\app1\web.config -> configuration for app1
c:\inetpub\wwwroot\app2\web.config -> configuration for app2. 

In the case the default web site root is off limits, you can create a root app to contain all other apps and store the common configuration there. 
c:\inetpub\wwwroot\myrootapp\web.config-> common configuration. c:\inetpub\wwwroot\myrootapp\app1\web.config -> configuration for app1 c:\inetpub\wwwroot\myrootapp\app2\web.config -> configuration for app2.

#5


-1  

If your Project 2 has a reference of Project 1 then Project1 may have a class with a ConnectionString property exposed.

如果您的项目2具有项目1的引用,则Project1可能具有暴露ConnectionString属性的类。

By the way, the "class" may read the connection string from the app.config

顺便说一句,“类”可以从app.config读取连接字符串