简体   繁体   English

如何在连接字符串上使用Web.Config转换?

[英]How do I use Web.Config transform on my connection strings?

In my current project, I have some connection strings that are valid for local development machines: 在我当前的项目中,我有一些对本地开发计算机有效的连接字符串:

<configuration>
  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="Data Source=localhost;Initial Catalog=MyDB;Integrated Security=SSPI"
  </connectionStrings>
....
</configuration>

How would I use the Web.Config transforms to convert from this expression to one valid for our production server? 我将如何使用Web.Config转换将这一表达式转换为对我们的生产服务器有效的表达式? The production server one would look something like: 生产服务器之一将类似于:

<configuration>
  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="Data Source=IPAddress,Port;Initial Catalog=SomeOtherDB;User ID=TopSecretUsername;Password=SecurePassword"
  </connectionStrings>
....
</configuration>

The syntax isn't obvious to me, and I'm completely failing at grokking the page on it. 语法对我来说并不明显,我完全无法浏览它的页面

This works for me but I too have found it to be a bit flakey at times. 这对我有用,但是我有时也觉得它有点苦涩。 You will need to create another file called Web.Config.Release and fill it with the following: 您将需要创建另一个名为Web.Config.Release的文件,并将其填充以下内容:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

  <connectionStrings>
    <add name="local" connectionString="Data Source=IPAddress,Port;Initial Catalog=SomeOtherDB;User ID=TopSecretUsername;Password=SecurePassword" 
    xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
  </connectionStrings>

  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />

  </system.web>
    <appSettings>
        <add key="default_db_connection" value="local" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
    </appSettings>
</configuration>

You shouldn't need to create a new file, it should be in the Solution Explorer, expand Web.config, and open Web.Release.config. 您不需要创建新文件,它应该在解决方案资源管理器中,展开Web.config,然后打开Web.Release.config。

Scott Allan has a good video on it here (under Configuration and Deployment > Config Transformations). 斯科特·艾伦(Scott Allan)在此处有一个很好的视频(在“配置和部署”>“配置转换”下)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在web.config中需要两个连接字符串吗? - Do I need two connection strings in web.config? 如何在Web.Config中的2个连接字符串之间切换(为DBML激活一个) - How can I switch between 2 Connection Strings in my Web.Config (Activate one for DBML) 在VS 2005中,如何为web.config生成连接字符串? - In VS 2005 How do I generate the connection strings for web.config? 我可以通过Web.Config连接字符串优化数据库连接吗? - Can I optimize my database connections via Web.Config connection strings? 在C#中,如何读取存储在web.config文件连接字符串中的连接字符串? - In C# , how can I read a connection string stored in my web.config file connection string? 如何转换web.config值? - How can I transform web.config values? 如何在我的C#代码中使用web.config连接字符串? - How to use web.config connection string in my C# code? 如何更改Web.config中数据库的连接字符串,这些字符串具有基于LOGIN数据库的Login_User表的数据库名称 - How can I change connection strings for database in my web.config that have database name based on Login_User table from LOGIN database 如何通过Windows应用程序访问Web.Config文件中的连接字符串 - How can I access the connection string in my Web.Config file through a Windows Application 如何转换这个web.config部分? - How to transform this web.config section?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM