Click here to Skip to main content
15,908,842 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have One Weird Query.

Is it possible to switch from one database to other database programmatically?

For Eg:

I have one Master Database containing 3 Companies.

Master Database:
CompanyID | Password | ConnStr
ABC0001 | ****** | MyConnStr1
PQR0001 | ****** | MyConnStr2
XYZ0001 | ****** | MyConnStr3

There is a Company Login Web Form Where the user has to give the company's credentials.

If these credentials are valid then the respective Connection string is taken from the master database and used to connect to the respective company's database (Lets Say Company ABC0001 has the database named dbABC0001).

So that the user from ABC0001 company can access the data from that database dbABC0001.

So Experts Will This Idea Work???

Please Help Me...
Posted

You can have one SqlConnection to check credentials and to retrieve the connection string. And the second one using that connection string to connect to company database
 
Share this answer
 
Before making SQL queries you must prepare a SQL connection, using a connection string. So you first must open a connection to the master database, check the credentials and get the connection string to the company's database. After that you must prepare new SQL connections with the connection string you obtained from the master database.

It will work, but using a separate master database with only one table and with only three rows appears a bit overblown. Instead I would use some kind of configuration file for this.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900