我有一个后台工作人员为我做一些数据库工作。我之所以使用后台工作者,是因为数据库工作占用了大量时间。所以代码第一次工作,但是当你继续更新数据库时,你会得到“无法保存;当前被另一个用户锁定。OledbException 未处理”。是解决这个问题的一种方法。这是我的代码
Private Sub DoSaveUsersAuditForUpdate_Click()
Dim myConn As OleDbConnection = frmLogIn.Conn
SyncLock myConn
'Insert Previous data before the Update
'myConn = frmLogIn.Conn
Dim cmdInsert As OleDbCommand = New OleDbCommand("Insert INTO UsersAudit (Reference,Comment) VALUES (?,?)", myConn)
cmdInsert.Parameters.Add("@Reference", OleDbType.WChar).Value = txtReference.Text
cmdInsert.Parameters.Add("@Comment", OleDbType.WChar).Value = "Update Existing Users Details"
myConn.Open()
cmdInsert.ExecuteNonQuery()
myConn.Close()
End SyncLock
Dim myKonn As OleDbConnection = frmLogIn.Conn
SyncLock myKonn
'Insert Current data after the Update
'myKonn = frmLogIn.Conn
Dim cmdInsertOriginal As OleDbCommand = New OleDbCommand("Insert INTO UsersAudit (Reference,Comment) VALUES (?,?)", myKonn)
cmdInsertOriginal.Parameters.Add("@Reference", OleDbType.WChar).Value = txtReference.Text
cmdInsertOriginal.Parameters.Add("@Comment", OleDbType.WChar).Value = "New User"
myKonn.Open()
cmdInsertOriginal.ExecuteNonQuery()
myKonn.Close()
End SyncLock
End Sub
我是这样称呼它的
Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
DoSaveUsersAuditForUpdate_Click()
End Sub
这一切都聚集在一起
Private Sub btnUpdate_Click(sender As System.Object, e As System.EventArgs)Handles btnUpdate.Click
BackgroundWorker1.RunWorkerAsync() '将 workerThread 作为新线程(AddressOf DoSaveUsersAuditForUpdate_Click)'workerThread.Start() DoUpdateUsers_Click(sender, e) UsersGridViewGridFormating() End Sub
Private Sub UsersGridViewGridFormating()
Dim myConn As OleDbConnection = frmLogIn.Conn
SyncLock myConn
Dim iSql As String = "SELECT * FROM Users WHERE UserDeleted =@UserDeleted"
Dim da As New OleDbDataAdapter(iSql, myConn)
da.SelectCommand.Parameters.AddWithValue("@UserDeleted", False)
dTable.Clear()
da.Fill(dTable)
myConn.Close()
Dim dv As DataView = New DataView(dTable)
dv.Sort = "UserName asc"
dTable = dv.ToTable
UsersMasterGridview.DataSource = dTable
UsersMasterGridview.Visible = True
UsersMasterGridview.Focus()
lblNumberofRecords.Text = dTable.Rows.Count & " Records"
End SyncLock
End Sub
任何帮助我正在使用的所有代码