ConfigurationManager.ConnectionStrings 从 machine.config 返回额外的连接字符串 [英] ConfigurationManager.ConnectionStrings returns extra connection string from machine.config

查看:24
本文介绍了ConfigurationManager.ConnectionStrings 从 machine.config 返回额外的连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Windows 服务,我需要 App.config 中定义的所有连接字符串的列表.

I'm writing a windows service and I need the list of all connection strings defined in App.config.

我使用 System.Configuration.ConfigurationManager.ConnectionStrings 来获取所有连接.

I Used System.Configuration.ConfigurationManager.ConnectionStrings to get all connections.

它总是返回这个额外的连接:

it always return this extra connection:

数据源=.SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;用户实例=true

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

我认为是在 machine.config 中添加的.

that I think is added in machine.config.

我的 App.config 是:

my App.config is:

<add name="AvmPortalConnectionString"
    connectionString="Data Source=.;Initial Catalog=AvmPortal;Integrated Security=True"
    providerName="System.Data.SqlClient" />

无论如何我只能获取 App.config 中定义的连接字符串(而不是 machine.config 中的那个)

Is there anyway that I can get only connection Strings defined in App.config(not the one in machine.config)

我可以删除它,但我想知道是否有更清洁的方法!

I can remove this, but I wonder if there is a cleaner way!

谢谢

推荐答案

尝试在添加前加一个clear:

Try adding a clear before the add:

<connectionStrings>
    <clear />
    <add name="AvmPortalConnectionString"
        connectionString="Data Source=.;Initial Catalog=AvmPortal;Integrated Security=True"
        providerName="System.Data.SqlClient" />
</connectionStrings>

这篇关于ConfigurationManager.ConnectionStrings 从 machine.config 返回额外的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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