Specify the active connection string to use in Web.Config

徘徊边缘 提交于 2019-12-11 01:55:38

问题


Is there a quick and easy way to set a connection string in Web.Config to be the active connection string?

I basically want to name my connection strings appropriately and then set one as active without having to switch out the names or re-compile my application.

Something like this:

<add name="Current" connectionString="{Local}"/>

<add name="Local" connectionString=[...]" />
<add name="RemoteOnMyServer" connectionString=[...]" />
<add name="RemoteAzure" connectionString=[...]" />

回答1:


I don't think that is possible the way you asked. But you can move the connection strings block to a separate file and then control which FILE is the active one:

<connectionStrings configSource="LocalDb.config"/>

Then you can have separate config files:

LocalDb.config
RemoteOnMyServer.config
RemoteAzure.config
<etc>

Each one of there would hold something like this:

<?xml version="1.0"?>
<connectionStrings>
    <add name="namedConnectionString" connectionString="Data Source=..." providerName="..." />
</connectionStrings>

Swithcing between them then becomes a matter of changing the configSource on the <connectionStrings /> element.




回答2:


Scott Hanselman has a good article describing using different configurations for different environments using the build setting in the compiler. I've used this with great success in some of my projects.

Have a look



来源:https://stackoverflow.com/questions/11243215/specify-the-active-connection-string-to-use-in-web-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!