473,657 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Different connection strings for different web servers

Is there a way in ASP.NET 2.0 to have different connection strings settings
in a web.config files that are dynamically used based upon the server that
the web application is running on?

For example, you have have two test web servers (TEST1 and TEST2) and a
production web server (PROD1). Each of these connection to a different
database for general data access and for membership/role providers.

<connectionStri ngs>
<add name="Test1DB" connectionStrin g="Data Source=Test1DBS erver;Initial
Catalog=MyApp;U ser Id=User;Passwor d=password;" />
<add name="Test2DB" connectionStrin g="Data Source=Test2DBS erver;Initial
Catalog=MyApp;U ser Id=User;Passwor d=password;" />
<add name="Prod1DB" connectionStrin g="Data Source=Prod1DBS erver;Initial
Catalog=MyApp;U ser Id=User;Passwor d=password;" />
</connectionStrin gs>

....

<providers>
<add name="Customize dMembershipProv ider"
type="System.We b.Security.SqlM embershipProvid er"
connectionStrin gName="Prod1DB"
applicationName ="MyApp"
minRequiredPass wordLength="5"
minRequiredNona lphanumericChar acters="0" />
</providers>

If this application is running on my production server (PROD1) all is well
as the provider is set to use the Prod1DB connection string. However, if the
application is running on my TEST1 web server, how do I programmaticall y
change the connectionStrin gName setting to the Test1DB connection string?

Thanks,

--
Pat B
BCC Software, Inc
A BÖWE BELL + HOWELL COMPANY
Jul 5 '07 #1
11 2273
"PatB" <Pa**@community .nospamwrote in message
news:D5******** *************** ***********@mic rosoft.com...
Is there a way in ASP.NET 2.0 to have different connection strings
settings
in a web.config files that are dynamically used based upon the server that
the web application is running on?
Yep - have a look at System.Environm ent.MachineName , and name your various
connection strings accordingly...

Alternatively, have a look at:
http://msdn2.microsoft.com/en-us/asp.net/aa336619.aspx

--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 5 '07 #2
Mark,

I have done exactly as you stated in ASP.NET 1.1 by preferencing my
connection string names with the machine name. Then in the Application_Sta rt
method I set a connection string variable to the appropriate connection
string from the web.config file.

This works great for a connection string that I am going to be using for
data access within my code.

But what about for the membership/role providers where the
connectionStrin gName is specified in the web.config file. Can I dynamically
change the provider's connectionStrin gName in the Application_Sta rt method?
If so how do I do that? (tried but couldn't figure it out).

Please pardon my lack of knowledge with ASP.NET 2.0. It's my first attempt
at using ASP.NET 2.0.

Thank you,

--
Pat B
BCC Software, Inc
A BÖWE BELL + HOWELL COMPANY
"Mark Rae [MVP]" wrote:
"PatB" <Pa**@community .nospamwrote in message
news:D5******** *************** ***********@mic rosoft.com...
Is there a way in ASP.NET 2.0 to have different connection strings
settings
in a web.config files that are dynamically used based upon the server that
the web application is running on?

Yep - have a look at System.Environm ent.MachineName , and name your various
connection strings accordingly...

Alternatively, have a look at:
http://msdn2.microsoft.com/en-us/asp.net/aa336619.aspx

--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 5 '07 #3
"PatB" <Pa**@community .nospamwrote in message
news:6D******** *************** ***********@mic rosoft.com...
I have done exactly as you stated in ASP.NET 1.1 by preferencing my
connection string names with the machine name. Then in the
Application_Sta rt
method I set a connection string variable to the appropriate connection
string from the web.config file.

This works great for a connection string that I am going to be using for
data access within my code.
OK.
But what about for the membership/role providers where the
connectionStrin gName is specified in the web.config file. Can I
dynamically
change the provider's connectionStrin gName in the Application_Sta rt
method?
If so how do I do that? (tried but couldn't figure it out).
Ah - I have to confess that I don't know for certain without some
experimenting of my own - I never use any of the Membership stuff...

However, Web Deployment Projects would *certainly* do this for you because
it allows different builds to replace various sections of the web.config
file...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 5 '07 #4
PatB,
This should be helpful:
http://www.eggheadcafe.com/tutorials...-configur.aspx

-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder( BETA): http://www.blogmetafinder.com

"PatB" wrote:
Is there a way in ASP.NET 2.0 to have different connection strings settings
in a web.config files that are dynamically used based upon the server that
the web application is running on?

For example, you have have two test web servers (TEST1 and TEST2) and a
production web server (PROD1). Each of these connection to a different
database for general data access and for membership/role providers.

<connectionStri ngs>
<add name="Test1DB" connectionStrin g="Data Source=Test1DBS erver;Initial
Catalog=MyApp;U ser Id=User;Passwor d=password;" />
<add name="Test2DB" connectionStrin g="Data Source=Test2DBS erver;Initial
Catalog=MyApp;U ser Id=User;Passwor d=password;" />
<add name="Prod1DB" connectionStrin g="Data Source=Prod1DBS erver;Initial
Catalog=MyApp;U ser Id=User;Passwor d=password;" />
</connectionStrin gs>

...

<providers>
<add name="Customize dMembershipProv ider"
type="System.We b.Security.SqlM embershipProvid er"
connectionStrin gName="Prod1DB"
applicationName ="MyApp"
minRequiredPass wordLength="5"
minRequiredNona lphanumericChar acters="0" />
</providers>

If this application is running on my production server (PROD1) all is well
as the provider is set to use the Prod1DB connection string. However, if the
application is running on my TEST1 web server, how do I programmaticall y
change the connectionStrin gName setting to the Test1DB connection string?

Thanks,

--
Pat B
BCC Software, Inc
A BÖWE BELL + HOWELL COMPANY
Jul 5 '07 #5
Peter,

That does sound as if it would solve my problem. I will have to experiment
with this as I build my site.

Thank you!

--
Pat B
BCC Software, Inc
A BÖWE BELL + HOWELL COMPANY
"Peter Bromberg [C# MVP]" wrote:
PatB,
This should be helpful:
http://www.eggheadcafe.com/tutorials...-configur.aspx

-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder( BETA): http://www.blogmetafinder.com

"PatB" wrote:
Is there a way in ASP.NET 2.0 to have different connection strings settings
in a web.config files that are dynamically used based upon the server that
the web application is running on?

For example, you have have two test web servers (TEST1 and TEST2) and a
production web server (PROD1). Each of these connection to a different
database for general data access and for membership/role providers.

<connectionStri ngs>
<add name="Test1DB" connectionStrin g="Data Source=Test1DBS erver;Initial
Catalog=MyApp;U ser Id=User;Passwor d=password;" />
<add name="Test2DB" connectionStrin g="Data Source=Test2DBS erver;Initial
Catalog=MyApp;U ser Id=User;Passwor d=password;" />
<add name="Prod1DB" connectionStrin g="Data Source=Prod1DBS erver;Initial
Catalog=MyApp;U ser Id=User;Passwor d=password;" />
</connectionStrin gs>

...

<providers>
<add name="Customize dMembershipProv ider"
type="System.We b.Security.SqlM embershipProvid er"
connectionStrin gName="Prod1DB"
applicationName ="MyApp"
minRequiredPass wordLength="5"
minRequiredNona lphanumericChar acters="0" />
</providers>

If this application is running on my production server (PROD1) all is well
as the provider is set to use the Prod1DB connection string. However, if the
application is running on my TEST1 web server, how do I programmaticall y
change the connectionStrin gName setting to the Test1DB connection string?

Thanks,

--
Pat B
BCC Software, Inc
A BÖWE BELL + HOWELL COMPANY
Jul 5 '07 #6
Hi Pat,

Yes, for ASP.NET 2.0 web application, if you want to use different
connectionstrin gs for different scenario, all the available approaches are
as below:

** add multiple connectionstrin gs and load them dynamically in code depend
on which one to use

** use Web Deployment project, thus, you can choose to set the certain c
onfiguration section(such as connectionstrin g) to a specifc value at
precompile/publish time. However, after that , the application still
statically load the fixed connectionstrin g from web.config file

http://weblogs.asp.net/scottgu/archi...06/429723.aspx

** .NET 2.0 configuration file provide a "configSour ce" which can let you
define the content of a configuration section in a separate file. Thus, you
can define multiple separate files(contains connectionstrin gs for different
scenarios) and change your main web.config file to use the certain one
depend on which one will be used:

#SectionInforma tion.ConfigSour ce Property
http://msdn2.microsoft.com/en-us/lib...sectioninforma
tion.configsour ce.aspx

#ASP Net - if statement in web.config file
http://www.velocityreviews.com/forum...webconfig-file
html

Anyway, so far web.config file doesn't direcctly support dynamic (or
if...else... like) loading mechanism.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 6 '07 #7
"Steven Cheng[MSFT]" <st*****@online .microsoft.comw rote in message
news:ZD******** ******@TK2MSFTN GHUB02.phx.gbl. ..
Hi Pat,
Your newsreader continues to post your messages in the wrong place in the
thread...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 6 '07 #8
Mark,

What are you talking about? They seem to be in the right place to me.

I am using the Microsoft browser newsreader.

--
Pat B
BCC Software, Inc
A BÖWE BELL + HOWELL COMPANY
"Mark Rae [MVP]" wrote:
"Steven Cheng[MSFT]" <st*****@online .microsoft.comw rote in message
news:ZD******** ******@TK2MSFTN GHUB02.phx.gbl. ..
Hi Pat,

Your newsreader continues to post your messages in the wrong place in the
thread...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 6 '07 #9
"PatB" <Pa**@community .nospamwrote in message
news:0C******** *************** ***********@mic rosoft.com...
What are you talking about? They seem to be in the right place to me.
Steven's reply to you appeared under my previous post i.e. as if he was
replying to me, not you...
I am using the Microsoft browser newsreader.
I'm using Windows Mail
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 6 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3310
by: S | last post by:
I have a database and an application in .net using it. In that database I have a stored procedure where is an import mechaism using ODBC driver and a .csv file. On my machine everything works fine, but on the other machine import fails with an exception Could not execute query against OLE DB provider 'MSDASQL'. Too few parameters. Expected 6.]
4
2038
by: Minh Tran | last post by:
In order to minimize the number of connection strings I have to use to access different databases on the same Sql Server, I was considering storing all stored procedures in just one database. I want to do this because connection pooling in my application - ASP.NET is based on this connection string. So if I need to access 6 different databases on one sql server & set 6 different connection strings, I end up creating 6 different...
5
1879
by: Mythran | last post by:
I have a test server and a development machine. I have SQL Server installed on both, the installations are pretty much identical. Both servers are in the same domain. When I run my application locally, I can connect to the local sql server as well as the sql server on the test server. The IIS settings on both the test server and local machine are the same (anon disabled, use integrated windows auth). Identity impersonate is on in...
3
1202
by: TS | last post by:
I have 2 sql server databases on 2 different servers, a web app, and a crystal reports interface. When the app uses 1 database and the reports datasource points to a different database, both databases on different servers, the application always comes up, but the report comes back saying no data. When i have both the app and reports pointing to the first server or both pointing to the second server, the reports and application work fine....
14
3490
by: WebMatrix | last post by:
Hello, I have developed a web application that connects to 2 different database servers. The connection strings with db username + password are stored in web.config file. After a code review, one developer suggested that it's a security flaw; therefore connection strings should be kept somewhere else or encrypted. My argument is that web.config file is protected by IIS and Windows security which is the case. And another argument is that...
6
2418
by: Joel H | last post by:
We have several settings in web.config that are different on the developer side than the production side. Our website sourcecode is under sourcesafe control, so before we code, we check out the web.config and change our connection strings to point to development databases, and change "debug=true". Before we deploy, we change everything back. We can't dynamically select the proper connection string in code because we are using the...
5
4760
by: zxo102 | last post by:
Hi, I am doing a small project using socket server and thread in python. This is first time for me to use socket and thread things. Here is my case. I have 20 socket clients. Each client send a set of sensor data per second to a socket server. The socket server will do two things: 1. write data into a file via bsddb; 2. forward the data to a GUI written in wxpython. I am thinking the code should work as follow (not sure it is feasible)...
5
2432
by: adeel shahid | last post by:
I m using .NET Crystal Report but i have to create report using different servers. right now the connection string is static using store procedure but i want to change the connection string at runtime using the storeprocedure. Can any one help that how can i create a report using a different server address using storeprocedure and dataset. Thanks Adeel Shahid
2
1119
by: jothikumar | last post by:
I have a fairly large webapp which is used by many customers. Each customer has its own database. I want to use the same webapp for all of the customers, but they still need to have separate databases and thus different connection strings. What is the best way to do this? I thought of this approach: 1. One database contains a table called Customers. This table contains all customers and what their connection string is. 2. When a...
0
8425
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8743
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8522
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.