如何在vb.net中使用一个连接连接两个数据库 [英] how to connect two database using one connection in vb.net

查看:96
本文介绍了如何在vb.net中使用一个连接连接两个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用两个不同的sql数据库和一个连接的vb.net项目。应用程序包含两个部分,即section1和section2。如果我更改section2数据应该保存在database2上,否则数据应保存在database1上。所以我需要有关连接类和部分的帮助。



章节随下拉菜单而变化。



希望会得到你的帮助。提前谢谢。

I am working on vb.net project with two different sql databases with one connection. Application consists of two sections which is section1 and section2. If I change section2 data should save on database2 or else data should save on database1. So I need help with connection class and section.

Sections change with pull down menu.

Hope will get help from you. Thank you in advance.

推荐答案

如果您正在谈论连接字符串,那么您无法使用一个连接字符串连接到两个数据库。连接字符串由以下内容组成。



If you are talking about a connection string, then you cannot connect to two databases with one connection string. A connection string is made up of following things.



  1. 服务器名称 - 数据库服务器
  2. 数据库名称 - 您要连接的数据库的名称
  3. ... 其他身份验证设置





这构成了你的连接字符串。因此,一旦您输入数据库名称,您将确保要连接到特定的数据库实例。这将如何连接到两个独立的数据库? 它不能 ......



但是......还有另一种转变,即使你连接到一个实例也是如此。您仍然可以连接到其他数据库,请考虑以下行:





This makes up your connection string. So, once you will enter a name of database you will ensure that you want to connect to a particular database instance. How would this connect to two separate databases? It cannot...

However... There is another turn around, in which even if you are connected to a single instance. You can still connect to other databases, think of the following line,

[DbName].dbo.[TableName]





它使用数据库和表的全名。现在,您将在SQL语句中编写此对象。它不仅仅是检查您自己的数据库(您使用该连接字符串连接),而是要求您的服务器返回该数据库表的响应。因此,如果要在为一个数据库使用连接字符串时使用其他数据库实例,则可以使用此选项。 但这并不意味着您现在也连接到其他数据库



因此,在您的部分中,您可以维护单个对象来检索数据。这就是为什么使用模型总是被认为是良好实践(IMO)。您的模型将包含数据库的所有连接,数据类型和其他对象(曾使用过Entity framework ?)。然后你可以通过调用这个Model类的实例或静态成员来使用它。



It uses full name for a database and table. Now when you will write this object in your SQL statement. It would not simply just check inside your own database (that you are connected to using that connection string) but instead it would ask your server to return the response for that database's table. So this can be usable if you want to use other database instances while using connection string for one database. But this does not mean that you are now connected to other databases also.

So in your sections, you can maintain a single object for retrieving the data. This is why using a Model is always considered good practice (IMO). Your model would contain all of the connections, data types and other objects for your database (Ever worked with Entity framework?). Which you can then use by calling instance or static members of this "Model" class.


直接说,这是不可能的。 Afzaal Ahmad Zeeshan [ ^ ]是正确的(见解决方案1)。



但是......即使您使用的是一个连接字符串,也可以随时更改数据库(在同一台服务器上)。 SqlConnection.ChangeDatabase方法非常简单 [ ^ ];)



另一种选择是使用这样的SQL命令:

Directly saying, it's impossible. Afzaal Ahmad Zeeshan[^] is right (see solution 1).

But... You can always change database (on the same server) even if you're using one connection string. It's quite easy with SqlConnection.ChangeDatabase method[^] ;)

Another option is to use SQL command like this:
USE DataBase2;
SELECT ...


这篇关于如何在vb.net中使用一个连接连接两个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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