Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2007

Tip: Looking for answers? Try searching our database.

Different connection strings for different web servers

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
PatB - 05 Jul 2007 14:12 GMT
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,

Signature

Pat B
BCC Software, Inc
A BÖWE BELL + HOWELL COMPANY

Mark Rae [MVP] - 05 Jul 2007 14:19 GMT
> 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.Environment.MachineName, and name your various
connection strings accordingly...

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

Signature

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

PatB - 05 Jul 2007 14:44 GMT
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,

Signature

Pat B
BCC Software, Inc
A BÖWE BELL + HOWELL COMPANY

> > Is there a way in ASP.NET 2.0 to have different connection strings
> > settings
[quoted text clipped - 6 lines]
> Alternatively, have a look at:
> http://msdn2.microsoft.com/en-us/asp.net/aa336619.aspx
Mark Rae [MVP] - 05 Jul 2007 15:04 GMT
> 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
[quoted text clipped - 4 lines]
> 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...

Signature

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

Steven Cheng[MSFT] - 06 Jul 2007 04:08 GMT
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.
Mark Rae [MVP] - 06 Jul 2007 09:02 GMT
> Hi Pat,

Your newsreader continues to post your messages in the wrong place in the
thread...

Signature

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

PatB - 06 Jul 2007 12:28 GMT
Mark,

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

I am using the Microsoft browser newsreader.

Signature

Pat B
BCC Software, Inc
A BÖWE BELL + HOWELL COMPANY

> > Hi Pat,
>
> Your newsreader continues to post your messages in the wrong place in the
> thread...
Mark Rae [MVP] - 06 Jul 2007 12:36 GMT
> 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

Signature

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

Steven Cheng[MSFT] - 09 Jul 2007 04:28 GMT
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.
Peter Bromberg [C# MVP] - 05 Jul 2007 15:56 GMT
PatB,
This should be helpful:
http://www.eggheadcafe.com/tutorials/aspnet/e54f2d32-72c4-4d2b-b180-c1f564b41f85
/some-aspnet-20-configur.aspx


-- Peter
Site:  http://www.eggheadcafe.com
UnBlog:  http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA):    http://www.blogmetafinder.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
[quoted text clipped - 30 lines]
>
> Thanks,
PatB - 05 Jul 2007 16:18 GMT
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!

Signature

Pat B
BCC Software, Inc
A BÖWE BELL + HOWELL COMPANY

> PatB,
> This should be helpful:
[quoted text clipped - 39 lines]
> >
> > Thanks,
Steven Cheng[MSFT] - 11 Jul 2007 10:59 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.