22

I store several different connection strings in my web.config for development and testing. All but one is commented out so I can change info as needed.

When I publish, I would like to replace everything (including comments) in the connectionStrings node with this:

<add name="myDb" connectionString="Data Source={SERVER};Initial Catalog=ManEx;User Id={USER};Password={PASSWORD};" providerName="System.Data.SqlClient"  />
<!--<add name="myDb" connectionString="Data Source={SERVER};Initial Catalog=ManEx;Integrated Security=True" providerName="System.Data.SqlClient" />-->

I know how to change the active string with this:

<add name="myDb"
     connectionString="Data Source={SERVER};Initial Catalog=ManEx;User Id={USER};Password={PASSWORD};"
     providerName="System.Data.SqlClient"
     xdt:Transform="Add" 
     xdt:Locator="Match(name)"/>

But I don't know how to clear out the comments I don't want and add the comment I do want.

Any ideas?

davids
  • 5,397
  • 12
  • 57
  • 94
  • 2
    Replacing / removing / adding comments isn't really the intended use of a transform. Get rid of the comments, put the proper connection strings in the proper transforms - and call it good. – Mike Aug 09 '14 at 12:37
  • 1
    Not only is it not the intended use of a transform - it just won't work. Comments are explicitly ignored, since they are, after all, comments and thus not part of the XML data, as far as the parser is concerned. They'll just get left alone. – dodexahedron Aug 09 '14 at 13:16
  • [Vote](https://visualstudio.uservoice.com/forums/121579-visual-studio-2015/suggestions/2578637-allow-inserting-comments-with-web-config-transform) for this feature to be include in Visual Studio – Tim Partridge Oct 23 '15 at 13:29

4 Answers4

32

Instead of transforming the string, or using "Remove" and "Insert" clean the section try using "Replace".

For example:

<connectionStrings xdt:Transform="Replace">

    <add name="myDb"
         connectionString="Data Source={SERVER};Initial Catalog=ManEx;User Id={USER};Password={PASSWORD};"
         providerName="System.Data.SqlClient" />

</connectionStrings>

You can configure this section exactly how you want it, even if that means you add new comments.

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
user2639740
  • 1,173
  • 10
  • 11
  • 1
    Look at that! This is exactly what I needed. – davids Dec 10 '14 at 16:28
  • 1
    Please be aware that this solution solves the problem, but it does so by replacing **the complete connectionstring part** of the configuration file. Any other connectionstring there will be lost. – Maarten Mar 14 '16 at 09:53
3

What I did based on this answer was the following:

  • Removed the existing connectStrings section in Web.config which contains commented out connection strings used during debug time;
  • Re-added the connectionStrings section with the correct connection string to be used when the app is deployed.

So in your Web.config transform file you have something like this:

<!-- Removes the existing connectionStrings section which contains internal connection strings used for debugging -->
<connectionStrings xdt:Transform="Remove">    
</connectionStrings>

<!-- Re-adding the existing connectionStrings section -->
<connectionStrings xdt:Transform="Insert">

<add name="MyConnectionStringName" connectionString="Data Source=CLUSTERSQL;Initial Catalog=MyDatabase;Integrated Security=True;multipleactiveresultsets=True" providerName="System.Data.SqlClient"  xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>

</connectionStrings>
Community
  • 1
  • 1
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • I didn't think about removing the entire section. That part works, but the Insert isn't working. The section isn't getting added back. – davids Dec 10 '14 at 16:05
  • Never mind, it is getting added, just to the bottom where no one will look for it. That will raise more concerns from my users. – davids Dec 10 '14 at 16:06
  • 1
    @davids: Exactly. It is added but at the bottom. I did not look further on how to keep it at the right place... in my case this won't make a difference. The important thing is that it's there. :) – Leniel Maccaferri Dec 10 '14 at 16:13
  • xdt:Transform="Replace" is better than Remove and Insert in this case. – kipusoep Mar 29 '18 at 11:29
0

In Visual Studio 2013, you can have several Web.config files.

Also, when you create a new project, VS creates 2 for you : Web.Debug.config and Web.Release.config. That way, you can have a different Web.Config for your debug project and for your release project

Deblaton Jean-Philippe
  • 11,188
  • 3
  • 49
  • 66
  • 1
    I tried using the Debug config to store my commented connection strings, but when I run it in debug, it doesn't use anything from the Debug file. – davids Aug 10 '14 at 06:48
  • 1
    @davids that is correct. For web projects, Visual Studio uses the main web.config and does not apply the transforms. However, if you use Visual Studio to deploy/publish or create a package of your project, then the transforms are applied. Still, that doesn't help for [F5] debugging. – Zarepheth Feb 04 '15 at 18:13
0

If you need to Add/Insert/Setattributes inside a replaced connectionstring (for example if you use deployments) you can nest the transformations to remove the comments and replace the attributes:

<connectionStrings xdt:Transform="Replace">
    <add name="connectionDatabase" 
         connectionString="#{ConnectionString}" 
         xdt:Transform="SetAttributes"
         xdt:Locator="Match(name)" />
</connectionStrings>
Psyonity
  • 54
  • 2