Different connection strings for different web servers

G

Guest

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.

<connectionStrings>
<add name="Test1DB" connectionString="Data Source=Test1DBServer;Initial
Catalog=MyApp;User Id=User;Password=password;" />
<add name="Test2DB" connectionString="Data Source=Test2DBServer;Initial
Catalog=MyApp;User Id=User;Password=password;" />
<add name="Prod1DB" connectionString="Data Source=Prod1DBServer;Initial
Catalog=MyApp;User Id=User;Password=password;" />
</connectionStrings>

....

<providers>
<add name="CustomizedMembershipProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="Prod1DB"
applicationName="MyApp"
minRequiredPasswordLength="5"
minRequiredNonalphanumericCharacters="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 programmatically
change the connectionStringName setting to the Test1DB connection string?

Thanks,
 
G

Guest

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_Start
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
connectionStringName is specified in the web.config file. Can I dynamically
change the provider's connectionStringName in the Application_Start 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
 
M

Mark Rae [MVP]

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_Start
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
connectionStringName is specified in the web.config file. Can I
dynamically
change the provider's connectionStringName in the Application_Start
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...
 
G

Guest

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
 
S

Steven Cheng[MSFT]

Hi Pat,

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

** add multiple connectionstrings 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 connectionstring) to a specifc value at
precompile/publish time. However, after that , the application still
statically load the fixed connectionstring from web.config file

http://weblogs.asp.net/scottgu/archive/2005/11/06/429723.aspx

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

#SectionInformation.ConfigSource Property
http://msdn2.microsoft.com/en-us/library/system.configuration.sectioninforma
tion.configsource.aspx

#ASP Net - if statement in web.config file
http://www.velocityreviews.com/forums/t121932-if-statement-in-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.
 
G

Guest

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
 
M

Mark Rae [MVP]

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
 
S

Steven Cheng[MSFT]

Hi Mark,

Thanks for your reminder, actually I did know that I reply to your last
message, I did it just want to keep all the messages in a single path
rather than break the thread into multiple subtrees.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Steven Cheng[MSFT]

Hi Pat,

How are you doing? Have you got the issue resolved?

If there is still anything we can help, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top