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

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

问题描述

我已经创建了一个控制台应用程序和一个 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天全站免登陆