从app.config获取ConnectionString [英] Getting a ConnectionString from app.config

查看:205
本文介绍了从app.config获取ConnectionString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web项目,该项目调用一个库项目(DataAccess)以从数据库中检索一些数据.我向DataAccess项目添加了一个App.config文件(添加->新项->应用程序配置文件),并添加了一个connectionString部分,如下所示:

I have a Web project which calls a library project (DataAccess) to retrieve some data from the database. I added an App.config file (Add -> New Item -> Application Configuration File) to the DataAccess project and added a connectionString section like this:

<configuration>
  <connectionStrings>
    <add name="local"
        connectionString="Data Source=.\sql2008;Initial Catalog=myDB;Integrated Security=True"
        providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

在DataAccess项目中,我具有BuildConnection方法:

In the DataAccess project, I have the BuildConnection method:

internal static SqlConnection BuildConnection()
{
    string connectionString = ConfigurationManager.ConnectionStrings["local"].ToString();
    return new SqlConnection(connectionString);
}

当我从Web项目中调用该方法时,它抛出一个空异常,抱怨本地"连接字符串不存在.在调试了一段时间之后,我将相同的连接字符串添加到了Web项目的Web.config中,现在它可以正常工作了.但是,问题在于我想将DataAccess项目与Web项目隔离开,换句话说,无论希望由谁调用,我都希望DataAccess项目使用其自己的app.config文件.这有可能吗?任何帮助将不胜感激.

When I call the method from the Web project, it throws a null exception complaining that the "local" connection string doesn't exist. After debugging it for a while I added the same connection string to the Web.config of the Web project, and now it works fine. The problem though is that I want to isolate the DataAccess project from the Web project, in other words, I want the DataAccess project to use its own app.config file no matter who calls it. Is this even possible? Any help would be appreciated.

推荐答案

在运行时,只有一个配置文件.因此活动项目的配置文件仅 经过考虑的.另外,您不能将类库项目作为活动/启动项目,即

at runtime, there is just one config file. so the config file of the active project is only considered. also, you cannot have a class library project as an active/startup project i.e.

假设您的解决方案中有4个项目,每个项目都有一个配置文件,那么当您运行该应用程序时,只会识别活动项目(即您的启动项目)的配置文件.

say you have 4 Projects in your solution, and each of them has a config file, then when you run the application, only the active project's(the one which is your startup project) config file is recognized.

现在您能做什么?

  1. 如果您只想隔离配置文件的各个部分,则可以在每个项目中都有一个配置文件,而这些文件又会在主要项目配置中引用,即

  1. if you just want to isolate the sections of the config file, then you can have config file in each of your Projects, which in turn, are referenced in the main projects config i.e.

Web.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <appSettings file="YourSettings.config">
      <add key="KeyToOverride" value="Original" />
      <add key="KeyToNotOverride" value="Standard" />
   </appSettings>
   <system.web>
        <!-- standard web settings go here -->
   </system.web>

YourSettings.config:

<appSettings>
   <add key="KeyToOverride" value="Overridden" />
  <add key="KeyToBeAdded" value="EntirelyNew" />
</appSettings>

详细了解此处

如果您想为活动项目本身提供单独的配置文件,则完全是另一回事. 这种丑陋的调整,但请在此处

if you want to have separate config files for your active project itself, than that's whole different story altogether. its kind of ugly tweak, but read about it here

这篇关于从app.config获取ConnectionString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆