当我想将数据从本地数据库更新到服务器数据时,我遇到了一个问题,替换了在本地数据库中修改的所有内容。我知道这可能很简单,但我对此一无所知,因此我们将不胜感激。
In my situation, I want to use a button to upload all modified data to the server database. Now I'm just using 2 databases at same server to do testing.
Private Sub btnUp_Click(sender As System.Object, e As System.EventArgs) Handles btnUp.Click
localconn.ConnectionString = lctext
serverconn.ConnectionString = sctext
Try
localconn.Open()
serverconn.Open()
Dim localcmd As New OdbcCommand("select a.acc_id as localid, a.acc_brcid, a.smartcardid, a.acc_created, a.acc_modified as localmodified, b.acd_firstname, b.acd_ic, b.acd_oldic, b.acd_race, b.acd_dob, b.acd_rescity, b.acd_resaddr1, b.acd_telmobile, b.acd_email, b.acd_telwork, b.acd_modified, b.acd_accid from nsk_account a inner join nsk_accountdetail b on a.acc_id = b.acd_accid", localconn)
Dim servercmd As New OdbcCommand("select c.acc_id, c.acc_brcid, a.smartcardid, c.acc_created, c.acc_modified, d.acd_firstname, d.acd_ic, d.acd_oldic, d.acd_race, d.acd_dob, d.acd_rescity, d.acd_resaddr1, d.acd_telmobile, d.acd_email, d.acd_telwork, d.acd_modified, d.acd_accid from nsk_account c inner join nsk_accountdetail d on c.acc_id = d.acd_accid", serverconn)
localcmd.CommandType = CommandType.Text
Dim rdr As OdbcDataReader = localcmd.ExecuteReader()
Dim thedatatable As DataTable = rdr.GetSchemaTable()
'localcmd.Parameters.Add("@localid", OdbcType.Int, "a.acc_id")
'localcmd.Parameters.Add("@localmodified", OdbcType.DateTime, "b.acd_modified")
Dim localid As String
Dim localmodi As String
localcmd.Parameters.AddWithValue("localid", localid)
localcmd.Parameters.AddWithValue("localmodified", localmodi)
For Each localid In thedatatable.Rows
Dim calldata As New OdbcCommand("SELECT acc_modified from nsk_account where acc_id ='" + localid + "'", serverconn)
Dim reader As OdbcDataReader = calldata.ExecuteReader
txtSDate.Text = reader("acc_modified").ToString
If localmodi <= txtSDate.Text Then
'do nothing, proceed to next data
Else
Dim ACCoverwrite As New OdbcCommand("Update nsk_account SET smartcardid = @mykad, acc_created = @created, acc_modified = @modify WHERE acc_id ='" + localid + "'", serverconn)
Dim DEToverwrite As New OdbcCommand("Update nsk_accountdetail SET acd_firstname = @name, acd_ic = @newic, acd_oldic = @oldic, acd_race = @race, acd_dob = @dob, acd_rescity = @city, acd_resaddr1 = @address, acd_telmobile = @phone, acd_email = @email, acd_telwork = @language, acd_modified = @detmodify WHERE acd_accid ='" + localid + "'", serverconn)
ACCoverwrite.ExecuteNonQuery()
DEToverwrite.ExecuteNonQuery()
End If
Next
MessageBox.Show("Upload success", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Finally
localconn.Close()
serverconn.Close()
End Try
End Sub
如有任何意见或建议,我们将不胜感激。
最佳答案
我希望你的意思是一张一张的。我没怎么读你的代码,但你明白了 - 你需要 2 个连接,但这里有 2 种不同的方法。
方式#1 - 你可以使用大量的数据(怎么说更好? - 不是很大)。您可以使用来自服务器的数据加载 DataTable
对象并更新更改的记录。您可以使用 DataAdapter
并发出 CommitChanges
- 所有更改/新行都将写入服务器。
NOTE: you need a mechanism that will reliably able to tell which rows are new and modified on your local DB. Are you OK if your PK in local DB will be different than on the server? You need to answer these questions. May be you need a special mechanism for PK locally. For example, add rows using negative PK integers, which will tell you that these rows are new. And use "ModifiedDate", which together with PK will tell if the row needs updating.
方式 #2 - 随时使用,即使数据量较大。取一个本地行并检查它。如果它是新的 - insert
,如果它存在并且“DateModified”已更改 - 执行 update
。有多种方法可以做到这一点。可以使用 SQL MERGE
语句等
但这是两种主要方式 - 直接行插入/更新和断开连接的更新/批量提交。
此外,您还可以批量执行此操作,使用事务 - 更新一些行 - 提交,然后开始新事务。如果应用程序在您更新时正在使用,这将有所帮助。
希望这些想法对您有所帮助。如果你做你所做的,你有什么地方
For Each localid In thedatatable.Rows
我不确定 localid
是什么。应该是
' prepare command before loop
sql = "Select * From Table where ID = @1"
' you will create parameter for @1 with value coming from
' row("ID")
Dim cmd As New .....
cmd.Parameters.Add(. . . . )
For Each row As DataRow In thedatatable.Rows
cmd.Parameters(0).Value = row("ID") ' prepare command upfront and only change the value
using reader as IDataReader = cmd.ExecuteReader(. . . . )
If Not reader.Read() Then
' This row is not found in DB - do appropriate action
Continue For
Else
' here check if the date matches and issue update
' Better yet - fill some object
End if
end using
' if you fill object with data from your row -here you can verify if
' update needed and issue it
. . . . . .
Next
关于mysql - 更新本地数据库所有修改的数据到服务器数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34522659/