Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change connection string automatically on build with Nant

Tags:

.net

nant

is it possible to change the connection string in my web.config automatically when build type is release using Nant? if so, how? thanks

like image 885
ryudice Avatar asked Dec 11 '09 05:12

ryudice


People also ask

How do I change my connection string?

In the Properties window, expand the Connection node. To quickly modify the connection string, edit the ConnectionString property, or click the down arrow on the Connection property and choose New Connection.

Where is connection string in app config?

Connection strings can be stored as key/value pairs in the connectionStrings section of the configuration element of an application configuration file.


2 Answers

I think you could use the xmlpoke task. For example, if your web.config is

<?xml version="1.0"?>
<configuration>
    <connectionStrings>
        <add name="myDb" connectionString="blah" providerName="blah"/>
    </connectionStrings>
</configuration>

Then you could add a task to your build file like this.

<xmlpoke 
    file="path_to_your_web_root\Web.config"
    xpath="/configuration/connectionStrings/add[@name='myDb']/@connectionString"
    value="your_connection_string" />

Oh, here is the documentation of the xmlpoke task. http://nant.sourceforge.net/release/latest/help/tasks/xmlpoke.html

like image 140
wenqiang Avatar answered Oct 02 '22 18:10

wenqiang


I assume that you want to do this in order to have the connection string point out a production environment rather than development or test environment when Nant builds the release code. I usually have a different approach to solve this scenario; keep the connection strings in a separate file. You can do this by using the configSource attribute:

<!-- point out a file containing the connectionStrings config section -->
<connectionStrings configSource="connections.config"></connectionStrings>

The connections.config file should look something like this:

<?xml version="1.0"?>
<connectionStrings>
    <add name="myDb" connectionString="{your connection string}"/>
</connectionStrings>

Since the connection strings rarely change in the production environment, the file connections.config can then typically be excluded from the deployment.

like image 20
Fredrik Mörk Avatar answered Oct 02 '22 18:10

Fredrik Mörk