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

查看:143
本文介绍了从外部配置文件读取连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个控制台应用程序以及一个app.config文件和Connections.config文件. app.config文件的connectionstring属性源指向Connections.config

I have created a console application and an app.config file and Connections.config file. The app.config file has a connectionstring property source pointing to the Connections.config

当我尝试在应用程序中读取连接字符串时,得到一个ConfigurationErrorException

When I tried to read the connection string in the application, I get a ConfigurationErrorException

这是我的主要方法.

static void Main(string[] args)
    {
        var settings = ConfigurationManager.ConnectionStrings;
        if (settings != null)
        {
            foreach (ConnectionStringSettings setting in settings)
            {
                Console.WriteLine(setting.ConnectionString);
            }
        }
    }

App.config文件

App.config file

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

Connections.config文件

Connections.config file

<?xml version="1.0" encoding="utf-8" ?>
<connectionStrings>
  <add name="SQLDBConnecion"
   providerName="System.Data.ProviderName"
   connectionString="" />
</connectionStrings>

在这里,我观察到两件事. 首先:如果指定configSource,我将无法读取连接字符串(引发异常.)

Here I observed two things. First: If I specify configSource I am unable to read the connection string (throwing exception.)

第二个:如果我在App.config文件中放入相同的连接字符串并尝试读取,则该代码有效,但得到了两个连接字符串(应该只返回一个为空字符串) 第一个连接字符串是sqlexpress这样的连接字符串

Second: If I put same connection string in App.config file and tried to read then the code is working but getting two connection string (which supposed to be return only one which is empty string) The first connection string is sqlexpress connection string like this

data source=.\SQLEXPRESS;Integrated Security=SSPI;
     AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true

它返回的第二个连接字符串是空字符串(这是预期的).

second connection string it returning is empty string (This is expected).

我想像我的场景一样从外部文件读取连接字符串.怎么做?我在这里想念什么?

I want to read connection string from external file like in my scenario. How to do that? What am I missing here?

推荐答案

MSDN说:

请勿包含任何其他元素,部分或属性.

Do not include any additional elements, sections, or attributes.

您需要删除XML编码.

You need to remove the XML encoding.

修改

此外,您需要将配置文件的属性设置为Copy to Output Directory = Copy if newerCopy always.

Also, you need to set the properties of your config file to Copy to Output Directory = Copy if newer or Copy always.

编辑2

要以Dave所说的为基础,请将clear元素添加到外部文件中.最终的Connections.config文件应如下所示:

To build on what Dave said, you add the clear element to your external file. Your final Connections.config file should look exactly like this:

<connectionStrings>
  <clear/>
  <add name="Name"
     providerName="System.Data.ProviderName"
     connectionString="Valid Connection String;" />
</connectionStrings>

这篇关于从外部配置文件读取连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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