C# 如何使用配置转换删除 ConnectionString
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8920451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove a ConnectionString using Config Transformations
提问by Didaxis
I have a Web.config with several ConnectionStrings
我有一个带有几个 ConnectionStrings 的 Web.config
<connectionStrings>
<add name="connStr1" connectionString="...
<add name="ConnStr2" connectionString="...
<add name="connStr3" connectionString="...
Is there a way using config transformations to remove a specific connectionstring? Something Like:
有没有办法使用配置转换来删除特定的连接字符串?就像是:
<connectionStrings>
<xdt:Remove connStr2?
Obviously no where near the correct syntax, but you get my drift...
显然没有接近正确的语法,但你明白我的意思......
采纳答案by M.Babcock
From the MSDN documentationon the subject:
从有关该主题的MSDN 文档中:
<configuration xmlns:xdt="...">
<connectionStrings>
<add xdt:Transform="Remove" />
</connectionStrings>
</configuration>
The Transform="Remove"is the magic you're looking for. There is also a Transform="RemoveAll"which you might be able to use in conjunction with a specific add(s).
这Transform="Remove"就是你正在寻找的魔法。还有一个Transform="RemoveAll"可以与特定的 add(s) 结合使用。
EDIT
编辑
On second thought you may also be able to combine the Locatorattributewith the Removedefined above to limit which elements you actually want to delete.
再想一想,您也可以将Locator属性与Remove上面定义的属性结合起来,以限制您实际要删除的元素。
More definitively:
更明确:
<configuration xmlns:xdt="...">
<connectionStrings>
<add xdt:Transform="Remove" xdt:Locator="XPath(configuration/connectionStrings[@name='ConnStr2'])" />
</connectionStrings>
</configuration>
Or similar should work.
或类似的应该工作。
回答by hyke20
This will remove a specific connection string based on its name.
这将根据其名称删除特定的连接字符串。
<configuration>
<connectionStrings>
<add name="ConnStr2" xdt:Transform="Remove" xdt:Locator="Match(name)" connectionString=" " />
</connectionStrings>
</configuration>
Note that the connectionStringvalue is not empty string, but is instead a space. Any non-empty value would do.
请注意,该connectionString值不是空字符串,而是一个空格。任何非空值都可以。

