我在C#中执行以下语句。 有两个表订单,数据。 订单是一个mysql数据库。 数据是MSSQL数据库。
insert into orders (orders_id, customers_id, customers_cid, customers_vat_id, customers_name, customers_email_address)
select
o.*
from
Test.dbo.orders o
where
not exists (
select 1
from
CobraDemoData.dbo.Data a
where
a.email0 = o.customers_email_address
)
我已经为两个数据库创建了连接字符串
MySqlConnection sqlConn = new MySqlConnection("server=localhost;User Id=root;Password = 123456;Persist Security Info=True;database=xtcommerce";)
SqlConnection con1 = new SqlConnection(@"Data Source=SUBASH-LAPTOP\COBRA;Initial Catalog=CobraDemoData;Integrated Security=True");
你能告诉我下一步该做什么吗?
任何帮助将不胜感激.. 谢谢, Subash ....
答案 0 :(得分:0)
打开两个读者,用相同的查询查询两个按相同的键排序的表,然后逐行进行比较,这样的话,(假设有两个连接--mssql和mysql:
var sc1 = mssql.CreateCommand();
var sc2 = mysql.CreateCommand();
sc1.CommandText = sc2.CommandText = "select id, name, email, phone from yourtable ORDER BY Id";
sc1.CommandTimeout = sc2.CommandTimeout = 0;
using (var r1 = sc1.ExecuteReader())
{
using (var r2 = sc2.ExecuteReader())
{
while (true)
{
bool read1 = r1.Read();
if (read1 ^ r2.Read())
throw new Exception("Doesn't match!");
if(!read1) {Console.WriteLine("MATCH!!!"); break;}
for (int i = 0; i < r1.FieldCount; i++)
{
if (r1.IsDBNull(i) ^ r2.IsDBNull(i))
throw new Exception("Doesn't match!");
if (string.Compare(r1[i].ToString(), r2[i].ToString(), StringComparison.InvariantCultureIgnoreCase) != 0)
throw new Exception("Doesn't match!");
}
}
}
}
然后,您可以基于此示例实现更新逻辑。
<强> BUT 强>
必须有一些唯一字段,用于标识两个表中的行