我正在尝试同步两个 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/