网络。配置转换——从connectionstring部分删除注释

时间:2021-06-03 20:20:43

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.

我在我的web中存储了几个不同的连接字符串。用于开发和测试的配置。除了一个,其他的都被注释掉了,所以我可以根据需要修改信息。

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

当我发布时,我想将connectionStrings节点中的所有内容(包括注释)替换为以下内容:

<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?

什么好主意吗?

4 个解决方案

#1


27  

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.

您可以准确地配置此部分,使其符合您的要求,即使这意味着您要添加新的注释。

#2


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;
  • 删除Web中现有的connectstring部分。配置,其中包含在调试期间使用的注释连接字符串;
  • Re-added the connectionStrings section with the correct connection string to be used when the app is deployed.
  • 在应用程序部署时,使用正确的连接字符串重新添加connectionStrings部分。

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

所以在您的Web。配置转换文件如下:

<!-- 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>

#3


1  

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

在Visual Studio 2013中,您可以有多个Web。配置文件。

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

另外,当您创建一个新项目时,VS将为您创建2:Web.Debug。配置和Web.Release.config。这样,你就有了一个不同的网络。用于调试项目和发布项目的配置

#4


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:

如果需要在替换的connectionstring中添加/插入/Setattributes(例如,如果您使用部署),您可以嵌套转换以删除注释并替换属性:

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

#1


27  

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.

您可以准确地配置此部分,使其符合您的要求,即使这意味着您要添加新的注释。

#2


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;
  • 删除Web中现有的connectstring部分。配置,其中包含在调试期间使用的注释连接字符串;
  • Re-added the connectionStrings section with the correct connection string to be used when the app is deployed.
  • 在应用程序部署时,使用正确的连接字符串重新添加connectionStrings部分。

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

所以在您的Web。配置转换文件如下:

<!-- 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>

#3


1  

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

在Visual Studio 2013中,您可以有多个Web。配置文件。

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

另外,当您创建一个新项目时,VS将为您创建2:Web.Debug。配置和Web.Release.config。这样,你就有了一个不同的网络。用于调试项目和发布项目的配置

#4


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:

如果需要在替换的connectionstring中添加/插入/Setattributes(例如,如果您使用部署),您可以嵌套转换以删除注释并替换属性:

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