English 中文(简体)
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-PCSQLEXPRESS;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-PCSQLEXPRESS 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

最佳回答

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

问题回答

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

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.

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.

Use this connection string:

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

It will work.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签