user175084
user175084

Reputation: 4640

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

Upvotes: 2

Views: 1943

Answers (6)

arup
arup

Reputation: 1

Use this connection string:

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

It will work.

Upvotes: 0

ProKiner
ProKiner

Reputation: 707

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.

Upvotes: 0

FrenchData
FrenchData

Reputation: 630

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.

Upvotes: 0

Dave Ward
Dave Ward

Reputation: 60590

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.

Upvotes: 2

Daniel A. White
Daniel A. White

Reputation: 191058

Try replacing XYX-PC with localhost provided the instance name is the same.

Upvotes: 3

LukLed
LukLed

Reputation: 31882

If database stands on the same server as IIS, you can use Data Source=localhost\sqlexpress

Upvotes: 2

Related Questions