mysql - 在vb.net中同步两个mysql数据库

标签 mysql sql database vb.net

我正在尝试同步两个 mysql 数据库。 我已成功使用此方法同步表:

Dim oldbcon As New MySqlConnection("server=localhost;database=psugsonline;userid=root;password=;")
Dim dbconn As New MySqlConnection("server=localhost;database=psugs;userid=root;password=;")

Dim da As New MySqlDataAdapter("select * from tbusers where sync='false'", dbconn)
Dim ds As New DataSet
ds.Clear()
da.Fill(ds)
If ds.Tables(0).Rows.Count > 0 Then
    Dim x As Integer
    For x = 0 To ds.Tables(0).Rows.Count - 1

        Dim instbuser As New MySqlCommand("insert into tbusers values('" & ds.Tables(0).Rows(x)(0).ToString & "','" & ds.Tables(0).Rows(x)(1).ToString & "','" & ds.Tables(0).Rows(x)(2).ToString & "','" & ds.Tables(0).Rows(x)(3).ToString & "','true')", oldbcon)
        oldbcon.Open()
        instbuser.ExecuteNonQuery()
        oldbcon.Close()
    Next
End If

但是这个方法只适用于我的一张表,如果表结构发生变化这个方法就不起作用了。

我必须同步 16 个表。我怎样才能实现这一目标?

最佳答案

Dim listOfConnectionStrings = New List(Of String)
listOfConnectionStrings.Add(connectionString1)
listOfConnectionStrings.Add(connectionString2)

等等

然后像您已经完成的那样连接到源数据库并构建查询字符串(即 INSERT INTO TABLE (X, Y, Z) VALUES (A, B C)

终于

For Each str In listOfConnectionStrings
'connect to the database and run the insert statement against it
Next

“如果表结构发生变化,此方法将不起作用”-表结构多久会发生变化?如果确实如此,请提前了解并修改您的代码以应对更改。

关于mysql - 在vb.net中同步两个mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20878248/

相关文章:

mysql - 为多个 ID 插入多个数据集

php - 数据库没有更新

android - Qt 如何连接到 android 操作系统中的现有 SQLite 文件?

c# - SqlDataAdapter输出变量问题C#

MySQL 查询与内连接返回空集

php - 如何比较两个选择?

mysql - 无法正确获取Mysql触发语句

mysql - SQL 一次匹配多个查询

Mysqli 默认返回不区分大小写的匹配项

sql - super 简单的查询需要 39 秒才能执行