2

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=[...]" />

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.