加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 如何在发布项目时交换连接字符串?

发布时间:2020-12-16 06:32:55 所属栏目:asp.Net 来源:网络整理
导读:我可以看到Web.config文件包含两个文件: -web.Debug.config-web.Release.config 在这个配置文件中有以下注释: In the example below,the “SetAttributes” transform will change the value of “connectionString” to use “ReleaseSQLServer” only wh
我可以看到Web.config文件包含两个文件:

-web.Debug.config
-web.Release.config

在这个配置文件中有以下注释:

In the example below,the “SetAttributes” transform will change the
value of “connectionString” to use “ReleaseSQLServer” only when the
“Match” locator finds an atrribute “name” that has a value of “MyDB”.

<connectionStrings>
  <add name="MyDB" 
    connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" 
    xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

什么是“匹配”定位器?
我已经在Web.config中有一个连接字符串所以我该如何设置它?
主web.config文件应该包含生产连接字符串还是反过来?
我正在寻找那些做过类似事情的人的挚爱

解决方法

我们正在使用xdt:Transform =“Replace”,它基本上替换了我们的开发DB的连接字符串,它完美地工作.见下文:

开发连接字符串(在您的情况下是web.Debug.config):

<connectionStrings>
  <add name="MyDB" connectionString="Data Source=DebugSQLServer;Initial Catalog=MyDebugDB;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

生产连接字符串(在您的情况下是web.Release.config):

<connectionStrings xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <add name="MyDB" connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" xdt:Transform="Replace" xdt:Locator="Match(name)" />
</connectionStrings>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读