Switching Between Test and Production Database

R

Rasool

Hi,

We have an asp.net application that we constanly use to query some data from
two servers (one prod and the other test). Everytime we have to go to
web.config and change the connection string to point to a different
database. Not to mention that while we are on the test database no one can
use that same application for prodution. We don't want to move the whole
application to a different location (i.e creating a new web site) for
maintenance issue. Is there a way to have two URL pointing to the same
asp.net application but using two web.config, so that by changing the URL
the only thing we are acually changing is the database? Thanks.
 
M

Matt Berther

Hello Rasool,

One solution might be to pass a querystring parameter indicating that you want to run the test database.

http://localhost/myapp/default.aspx?db=test

You could set a session variable basedon this in Session_Start and have a database connection factory object that pulls the appropriate string from the web.config.

class ConnectionFactory
{
public static string GetConnectionString()
{
string connString = ConfigurationSettings.AppSettings["prodKeyName"];

if (Session["db"] == "test")
{
connString = ConfigurationSettings.AppSettings["testKeyName"];
}
return connString;
}
}

Anywhere that you are using the connection string, simply change the code to call ConnectionFactory.GetConnectionString(). This would allow you to work in additional databases as you need. Let's say you want to incorporate a staging database.

None of your other code would change, you'd simple add the correct check to the factory method.
 
D

Dan Brussee

web.config is just a convenient place to keep connection strings. You
can do whatever you need to do to get connected. Two web.config
settings and a session var, or whatever you decide to do. Maybe have a
master database that points to other databases?
 
W

WJ

You can create a sub-web under the current production web site. Then build a
separate "Web.Config" file that references your Test DB under the sub-web
folder. Finally, create a link label from your home page (production) to
lead your users into the test sub-web site where your page loader would open
its own database using its own Web.Config file.

John
 
G

Guest

Why oh why are you hardcoding the servername into the web.config file? A solution I implement is to have an Access database containing a list of all servers my apps might need to use. For me, this database is on a network share that everyone has access to, but for a web app it could just as easily be on the web server's hard disk - c:\inetpub\wwwroot\data\servers.mdb for instance
You can then easily have a page from which the user can select the server they want to use, and you can build the connection string dynamically from there
But hard-coding the server name in the web.config... that's just, well - bad.
 
M

Matt Berther

Hello =?Utf-8?B?QjBuag==?=,

Why oh why are you hardcoding the servername into the web.config file?
A solution I implement is to have an Access database containing a list
of all servers my apps might need to use. For me, this database is on
a network share that everyone has access to, but for a web app it
could just as easily be on the web server's hard disk -
c:\inetpub\wwwroot\data\servers.mdb for instance.

I disagree. This is exactly what the web.config is for (things an application needs to run).
You can then easily have a page from which the user can select the
server they want to use, and you can build the connection string
dynamically from there.

Why couldnt this be done with an IConfigurationSectionHandler that lists all the servers. This way, you could use the easier ConfigurationSettings API and also not worry about remembering to deploy an additional file?
 
W

WJ

I never offerred whether it is bad or good. It is just a "way" and it is
possible or just an opinion ...:)

John

B0nj said:
That's even worse than the other two answers - doesn't he have to then
maintain a separate subweb for each and every database he wants to support?
 

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,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top