Click here to Skip to main content
15,886,095 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I have a requirement like, developed an ASP.net web application. I want to host this application for different companies with seperate database, but website shall be hosted only one copy in IIS. so, based on user login, the application should connect different database server.

For Ex: if user "xxx" login with credential and belogs to "ABC" company and the database is "ABC", then ABC data need to display on the web page.

if user "XYZ" login with credential and belogs to "HGJ" company and the database is "HGJ", then HGJ data need to display on the web page.

Can any one give good solution for the same.

Thanks in Advance
Ravikrishna
Posted
Updated 3-Mar-11 0:57am
v2

This is not difficult to implement. In fact I have implemented an application that can talk to different database.

The first thing to sort out is, where is your login credentials to be authenticated. That is to say, when user "xxx" tried to login, do you have a centralized User's database or is it each user is going to be authenticated to their respective database.

If it is the former case then you need to add the company into the user's table, so when the user is authenticated you know which company s/he belongs to. If it is the later then you need to take company part of the user/password combination, so you know which database you to go after.

Once the user's company is identified it is straight forward from there on. May be you need to keep some sort of dictionary or map that maps company to database.

If you can work with single database, working with multiple is no different. You will need a connection string for each database. There rest is, as they say it, history.
 
Share this answer
 
Comments
Member 2823421 3-Mar-11 7:32am    
Hi Yousuf,

Thanks for the reply.I Agree with you r Scenario. Here I have another issue. Same username may be in different companies then which need to select. there will be some ambiguity
kyithar 25-Mar-20 1:59am    
Hi,
about that ambiguity, in my projects, I made not allow to register same username.
Member 12465879 27-Nov-20 20:54pm    
what about performance when multiple companies users working on application in same time ?
Hi,

You have already describe in your question that how to do. But also see following table structure for your requirement.

MainTable
---------
Username
password
Company
Connectionstring


when you check usename and password in this table according to that you have to take a connection string and connect to that database.

In shot in ConnectionString field you have to store different connection string for different database.
 
Share this answer
 
web.config

XML
<connectionStrings>
    <add name="ConnectionString1" connectionString="Data Source=MUTHU-PC\SQLEXPRESS;Initial Catalog=yourFirstDataBaseName;Integrated Security=False;User ID=sa5556;Password=5555" providerName="System.Data.SqlClient" />
    <add name="ConnectionString2" connectionString="Data Source=MUTHU-PC\SQLEXPRESS;Initial Catalog=yourSecondDataBaseName;Integrated Security=False;User ID=sa5556;Password=5555" providerName="System.Data.SqlClient" />
  </connectionStrings>


in code behind

public string btnlogin(role_id)
{
if(role_id==1)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString);

con.Open();
fill ur requirement

con.Close();

}
elseif(role_id==2)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString);
con.Open();
fill ur requirement

con.Close();
}

}
 
Share this answer
 
go to multitenant concept
 
Share this answer
 

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