切换数据库连接 [英] Switch Database Connection

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

问题描述

我有一个奇怪的查询.

是否可以通过编程方式从一个数据库切换到另一数据库?

例如:

我有一个包含3个公司的主数据库.

主数据库:
公司编号|密码| ConnStr
ABC0001 | ****** | MyConnStr1
PQR0001 | ****** | MyConnStr2
XYZ0001 | ****** | MyConnStr3

有一个公司登录Web表单,用户必须在其中提供公司的凭据.

如果这些凭据有效,那么将从主数据库中获取相应的连接字符串,并将其用于连接到相应公司的数据库(假设公司ABC0001的数据库名为dbABC0001).

这样,ABC0001公司的用户就可以访问该数据库dbABC0001中的数据.

因此专家会将此想法奏效吗?

请帮助我...

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

推荐答案

您可以使用一个SqlConnection来检查凭据并检索连接字符串.第二个使用该连接字符串连接到公司数据库
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


在进行SQL查询之前,您必须使用连接字符串准备一个SQL连接.因此,您首先必须打开与主数据库的连接,检查凭据并获取与公司数据库的连接字符串.之后,您必须使用从主数据库获得的连接字符串来准备新的SQL连接.

它将起作用,但是使用只有一个表且只有三行的单独的主数据库似乎有点夸大其词.相反,我会为此使用某种配置文件.
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.


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

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