一个具有多个数据库的应用 [英] One application with multiple databases

查看:86
本文介绍了一个具有多个数据库的应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个要求,就是开发了一个ASP.net网络应用程序。我想为具有单独数据库的不同公司托管此应用程序,但网站只能在IIS中托管一个副本。因此,基于用户登录,应用程序应该连接不同的数据库服务器。



对于Ex:如果用户xxx登录凭证并且警告ABC公司和数据库是ABC,然后ABC数据需要显示在网页上。



如果用户XYZ使用凭证登录并且警告HGJ公司和数据库是HGJ,然后HGJ数据需要显示在网页上。



任何人都可以给出相同的解决方案。



在此先感谢

Ravikrishna

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

推荐答案

这并不难实现。事实上,我已经实现了一个可以与不同数据库通信的应用程序。



首先要解决的是,您的登录凭据在哪里进行身份验证。也就是说,当用户xxx尝试登录时,您是否拥有一个集中的用户数据库,或者每个用户是否都要对其各自的数据库进行身份验证。



如果是前一种情况,那么您需要将公司添加到用户的表中,因此当用户通过身份验证时,您知道他/她所属的公司。如果是晚些时候你需要公司的一部分用户/密码组合,所以你知道你要去哪个数据库。



一旦用户的公司是从那里确定它是直接的。可能你需要保留某种将公司映射到数据库的字典或地图。



如果你可以使用单个数据库,那么使用多个数据库也没有什么不同。您将需要每个数据库的连接字符串。正如他们所说,其余就是历史。
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.






你已经在你的问题中描述了如何做。但也请参阅以下表格结构以满足您的要求。



MainTable

---------

用户名

密码

公司

Connectionstring




根据您必须连接字符串并连接到该数据库,检查此表中的用户名和密码时




在ConnectionString字段中拍摄你必须为不同的数据库存储不同的连接字符串。
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.


web.config



web.config

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



<代码背后的




public string btnlogin(role_id)

{

if (role_id == 1)

{

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [ConnectionString1]。ConnectionString);



con.Open();

填写你的要求



con.Close();



}

elseif(role_id == 2)

{

SqlConnectio n con = new SqlConnection(ConfigurationManager.ConnectionStrings [ConnectionString2]。ConnectionString);

con.Open();

填写你的要求



con.Close();

}



}



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();
}

}


这篇关于一个具有多个数据库的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆