[英]How to connect to two databases in one connection string in C#?
Normally, when I need to connect to a database using C#, I would use the following command routines below: 通常,当我需要使用C#连接到数据库时,我将使用以下命令例程:
- define a mysql connection. - 定义一个mysql连接。
- open a mysql connection. - 打开一个mysql连接。
- define a sql statement / query. - 定义一个sql语句/查询。
- use MySqlCommand to execute the query. - 使用MySqlCommand执行查询。
Sample codes: 示例代码:
string con1 = "server=<db1 IP>;User Id=user;password=password;Persist Security Info=True;database=db1";
string con2 = "server=<db2 IP>;User Id=user;password=password;Persist Security Info=True;database=db2";
MySqlConnection cn1 = new MySqlConnection(con1);
MySqlConnection cn2 = new MySqlConnection(con2);
MySqlCommand com
cn1.Open();
string sql = "some query";
com = new MySqlCommand(sql, cn1);
com.executeNonQuery();
cn1.Close();
My problem above is on the part where I use the MySqlCommand command because it is where a database connection is indicated so that it will now which database to query to like 我上面的问题是在我使用MySqlCommand命令的部分,因为它是指示数据库连接的位置,以便它现在将查询哪个数据库
MySqlCommand com = new MySqlCommand(sql, con);
where sql is a sql statement and con is a connection to be used for the query. 其中sql是sql语句,con是用于查询的连接。
How do I query two databases in one sql statement? 如何在一个sql语句中查询两个数据库?
Consider the following: (I'm using MySQL) 请考虑以下事项:(我正在使用MySQL)
- I have two databases, db1 and db2.
- db1 is located in City A
- db1 is located in City B
- Both databases have one table (tbl) and they both have the same structure.
- Table structure for tbl:
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| id | int(9) | NO | PRI | | |
| ref_no | int(9) | NO | | | |
| name | varchar(10) | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+
- I want to run a query on db1.tbl against db2.tbl
- Example query: "select ref_no from db1.tbl where ref_no not in (select ref_no from db2.tbl)"
Or is there another way for this kind of problem?... 或者还有另一种解决这类问题的方法吗?......
string con = "server=localhost;user=root;pwd=1234;";
using (MySqlConnection cn1 = new MySqlConnection(con))
{
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = cn1;
cn1.Open();
cmd.CommandText = sql;
MySqlDataAdapter da = new MySqlDataAdapter();
....
}
sql statement: sql语句:
select a.ref_no from db1.tbl a where a.ref_no not in (select b.ref_no from db2.tbl b)
You can query multiple database at a time. 您可以一次查询多个数据库。
Update 更新
I think the only option is create 2 connections at the same time and pass the data between the 2 server through C#. 我认为唯一的选择是同时创建2个连接,并通过C#在2服务器之间传递数据。
What you are looking for is the Federated Storage Enginge 您正在寻找的是Federated Storage Enginge
On one of the server (or even both), you can create a placeholder table that is transparently routed by the MySQL server to the other MySQL server. 在其中一个服务器(甚至两个)上,您可以创建一个占位符表,该表由MySQL服务器透明地路由到另一个MySQL服务器。
It allows you to run your query on just one server and your server will contact the other server for the data it needs. 它允许您只在一台服务器上运行查询,您的服务器将与其他服务器联系以获取所需的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.