473,394 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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.

<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,

--
Pat B
BCC Software, Inc
A BÖWE BELL + HOWELL COMPANY
Jul 5 '07 #1
11 2254
"PatB" <Pa**@community.nospamwrote in message
news:D5**********************************@microsof t.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.Environment.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_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
"Mark Rae [MVP]" wrote:
"PatB" <Pa**@community.nospamwrote in message
news:D5**********************************@microsof t.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.Environment.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**********************************@microsof t.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_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...
--
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.

<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,

--
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.

<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,

--
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
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/archi...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/lib...sectioninforma
tion.configsource.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.comwrote in message
news:ZD**************@TK2MSFTNGHUB02.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.comwrote in message
news:ZD**************@TK2MSFTNGHUB02.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**********************************@microsof t.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
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.

Jul 9 '07 #11
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.

Jul 11 '07 #12

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

Similar topics

3
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...
4
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...
5
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...
3
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...
14
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,...
6
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...
5
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...
5
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...
2
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.