简体   繁体   中英

connection string in web config

I have a web application which i make on my local host and publish it on different servers.

in the web config of this application i have connectionstrings property like:

<connectionStrings>
<add name="MyConnectionString" connectionString="Data Source=XYZ-PC\SQLEXPRESS;Initial Catalog=SumooHServerDB;Integrated Security=True" providerName="System.Data.SqlClient"/>

Now connectionstring data source has the name of my server and when ever i publish it and run this application on different server i have to change XYZ-PC\\SQLEXPRESS to the name of the server..

Is there a way i dont have to do this as it does not feel right..

any suggestions..

thanks

如果实例名称相同,请尝试用localhost替换XYX-PC

如果数据库与IIS位于同一服务器上,则可以使用Data Source=localhost\\sqlexpress

I like to use configSource to pull the connection string out into a separate file, as explained here*: http://stevenharman.net/blog/archive/2007/06/07/tip-put-connection-strings-in-their-own-configuration-file.aspx

That way, you can configure each server's connectionStrings.config once, but continue updating their web.config files with a single version that works for all of them.

* Except, I usually name it connectionStrings.config, so it's more obvious for maintenance by others.

The following article could be a solution to your question : Handling Multiple Environment Configurations with .NET 2.0

Basically, the idea is to use the fact that in your config file you can indicate that some sections have to be read from an external file. Than during the build of your project you copy the right external file according to your environment. I think the link will explain this better.

Use this connection string:

name="MyConnectionString" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SumooHServerDB;Integrated Security=True" providerName="System.Data.SqlClient"

It will work.

Take a look at Web Deployment projects.

In addition to letting you merge everything into a single assembly, it give you the option of doing section-based web.config replacements. We use this on our build server to alter the standard web.config for running in the test environment. As a bonus, you're not limited to just changing connection strings. Anything in the web.config is fair game.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM