Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change connectionstrings with msbuild

Tags:

c#

msbuild

I am trying to change the connectionString in my web.config using MSBUILD on a Teamcity server. Previously I have use the attribute in a target that called this:

<PropertyGroup>
<UpdateWebConfigCode>
 <![CDATA[
  public static void ScriptMain()
  {
   XmlDocument wcXml = new XmlDocument();
   wcXml.Load(@"TCM.MVC.UI\Web.config");

   XmlElement root = wcXml.DocumentElement;
   XmlNodeList connList = root.SelectNodes("//connectionStrings/add");
   XmlElement elem;

   foreach (XmlNode node in connList)
   {
    elem = (XmlElement)node;

    switch (elem.GetAttribute("name"))
    {
     case "TCMBaseConnectionString":
      elem.SetAttribute("connectionString", "Data Source=server-name;Initial Catalog=TCMCentral;User ID=user;Password=something");
      break;

    }
   }

   wcXml.Save(@"TCM.MVC.UI\Web.config");          

  }
 ]]>
</UpdateWebConfigCode>

I would then call it in the target:

<Target Name="UpdateWebConfig">         
   <Script Language="C#" Code="$(UpdateWebConfigCode)" Imports="System.Xml" /> 
</Target>

But this keeps throwing an error. I realise this is probably a bit out of date but can't find anything to replace it .... any suggestions?

like image 503
abarr Avatar asked Jun 30 '26 10:06

abarr


1 Answers

I ended up using the MSBuildCommunityTasks XmlUpdate attribute. Below is my target:

<Target Name="UpdateWebConfig">        
    <XmlUpdate XmlFileName="C:\TCM.NET\Current\TCM.MVC.UI\web.config" XPath="configuration/connectionStrings/add[@name='TCMBaseConnectionString']/@connectionString" Value="Data Source=server-name;Initial Catalog=TCMCentral;User ID=user;Password=something" />
</Target>

This works great for me.

like image 145
abarr Avatar answered Jul 01 '26 23:07

abarr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!