如何使用 mysql.data 通过 C# 将 MySQL table 从一台服务器复制到另一台服务器?
How do I copy a MySQL table from one server to another server via C# using mysql.data?
我正在尝试将 table 及其数据从服务器 1 复制到服务器 2。如果 table 存在,请在复制前删除 table。但是我被卡住了,找不到继续的答案。请帮忙。
/*connect the two servers*/
MySqlConnection conn_DB1 = new MySqlConnection(connString_DB1);
MySqlConnection conn_DB2 = new MySqlConnection(connString_DB2);
/*get the data from database.table on server 1*/
MySqlDataAdapter adp_DB1 = new MySqlDataAdapter($"select * from {from_db}.{from_table}", conn_DB1);
DataSet theDataSet_DB1 = new DataSet();
adp_DB1.Fill(theDataSet_DB1, $"{from_db}.{from_table}");
/*drop the database.table on server 2*/
DataTable tmp = new DataTable();
MySqlDataAdapter adp_DB2 = new MySqlDataAdapter($"DROP TABLE {to_db}.{to_table}", conn_DB2);
adp_DB2.Fill(tmp);
/*How to: copy the schema and data from server 1 to server 2?*/
我找到了解决方案
1 - 从服务器 1 获取创建脚本
SHOW CREATE TABLE database.table
2- 在服务器 2
中删除 table
DROP TABLE IF EXISTS to_db.to_table
3- select 来自服务器 1 的数据并填充服务器 2
select * from from_db.from_table
4 - 将 table 从 更改为新名称并执行寄存器操作的一些代码;
create_command.Replace($"CREATE TABLE `{from_table}`", $"CREATE TABLE `{to_table}`");
'INSERT INTO...'
我正在尝试将 table 及其数据从服务器 1 复制到服务器 2。如果 table 存在,请在复制前删除 table。但是我被卡住了,找不到继续的答案。请帮忙。
/*connect the two servers*/
MySqlConnection conn_DB1 = new MySqlConnection(connString_DB1);
MySqlConnection conn_DB2 = new MySqlConnection(connString_DB2);
/*get the data from database.table on server 1*/
MySqlDataAdapter adp_DB1 = new MySqlDataAdapter($"select * from {from_db}.{from_table}", conn_DB1);
DataSet theDataSet_DB1 = new DataSet();
adp_DB1.Fill(theDataSet_DB1, $"{from_db}.{from_table}");
/*drop the database.table on server 2*/
DataTable tmp = new DataTable();
MySqlDataAdapter adp_DB2 = new MySqlDataAdapter($"DROP TABLE {to_db}.{to_table}", conn_DB2);
adp_DB2.Fill(tmp);
/*How to: copy the schema and data from server 1 to server 2?*/
我找到了解决方案
1 - 从服务器 1 获取创建脚本
SHOW CREATE TABLE database.table
2- 在服务器 2
中删除 tableDROP TABLE IF EXISTS to_db.to_table
3- select 来自服务器 1 的数据并填充服务器 2
select * from from_db.from_table
4 - 将 table 从 更改为新名称并执行寄存器操作的一些代码;
create_command.Replace($"CREATE TABLE `{from_table}`", $"CREATE TABLE `{to_table}`");
'INSERT INTO...'