问题描述
是否有可能改变连接字符串(或者服务器主机)选择依靠网络上发布的个人资料?也许使用或其他天晓得?
Is it possible to change connection string (or just server host) relying on selected web publish profile? Maybe using Web.config transform or someway else?
我的意思是个人资料测试改变连接字符串MyConnString(发表在Web.config文件),以数据源='example.com,14333; ......
和配置文件生产 - 以数据源= / SQLEx preSS; ......
I mean for profile "Test" change connection string "MyConnString" (in published Web.config) to "Data Source='example.com,14333;..."
and for profile "Production" - to "Data Source=./SQLExpress;..."
推荐答案
这也正是是为创造了什么web配置变换。您在您的文章中提供的链接有专门做这个连接字符串的演练。
This is exactly what web config transforms were created for. The link you provided in your post has a walkthrough of doing this specifically for connection strings.
要与转换开始,在项目资源管理器右键点击你的web.config文件,并选择添加配置变换。假设你有ConfigA和ConfigB在解决方案的配置,会有增加了两个新的文件,Web.ConfigA.config和Web.ConfigB.config。
To start with the transforms, right-click your web.config file in the project explorer and choose "Add Config Transforms". Assuming that you have ConfigA and ConfigB in your solution configuration, there will be two new files added, Web.ConfigA.config and Web.ConfigB.config.
如果你打开这些新文件,他们将pretty除了一堆意见空。他们实际上包含其中一个连接字符串,例如,你可以使用,虽然 - 它看起来是这样的:
If you open these new files, they'll be pretty empty except for a bunch of comments. They actually contain a connection string example in them that you can use though - it looks like this:
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
取消注释本节,与名属性更改为在基本web.config文件中的连接字符串的名称。设置的connectionString属性设置为您要用于ConfigA的实际值。因此,像这样的:
Uncomment this section, and change the "name" property to the name of the connection string in the base web.config file. Set the "connectionString" property to the actual value that you want to use for ConfigA. So, like this:
<connectionStrings>
<add name="myConnectionString"
connectionString="Data Source=ConfigASqlServer;Initial Catalog=ConfigADatabase;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
重复该过程的Web.ConfigB.config文件,与ConfigB所需的连接字符串。
Repeat the process for the Web.ConfigB.config file, with the desired connection string for ConfigB.
现在,当您使用Visual Studio中发布命令,它会自动变换基地web.config文件,并设置的ConnectionString属性你,当你发布在任何配置。
Now when you use the Publish command in visual studio, it will automatically transform the base web.config file, and set the "connectionString" attribute to whatever configuration you're in when you publish.
这篇关于在每个VS2010发布配置不同的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!