How to change connection string automatically on build with Nant
Asked Answered
H

4

8

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

Handicraft answered 11/12, 2009 at 5:24 Comment(0)
P
18

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

Placentation answered 11/12, 2009 at 6:14 Comment(1)
It does not seem to work when connectionString contain spaces :(Clem
K
5

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.

Kerman answered 11/12, 2009 at 6:1 Comment(1)
Is there a similar way to set database/datasource/connectionString in sqlmap.config ?Clem
F
2

Another alternative is to use a template where you have a token in place of the connection string e.g.

<connectionString>
   <add name="myDb" connectionString="@CONNECTION_STRING@" />
</connectionStrings>

Then use filterchains to replace this with the appropriate string.

<copy file="Web.config.template" tofile="Web.config" overwrite="true">
    <filterchain>
        <replacetokens>
            <token key="CONNECTION_STRING" value="${ConnectionString}" />
        </replacetokens>
    </filterchain>
</copy>

The value of the ConnectionString property will vary according to the build type.

Filterchains are described in the Nant Documentation

Ferroelectric answered 16/12, 2009 at 16:49 Comment(1)
Wonderfully simple extension solution.Existence
P
1

I use a simple way. I prepared many versions of the config file, each contains its own connection strings (DEV, TEST, PRODUCTION). In NANT script, when I author different targets, I copy the specific config file to overwrite the default one.

Polysyllabic answered 11/12, 2009 at 7:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.