我尝试使用DataAdapter.Update
在数据库中进行批量更新,用户可以选择取消操作。
问题:当用户点击取消按钮时,如何停止更新操作?
我的代码::
Log("@@@@@ Saving BMPImages paths to the database @@@@@")
objCommandBuilder = New SqlCommandBuilder(daImages)
SavingImageObj.MyProgressBar.Maximum = tblImages.GetChanges.Rows.Count
AddHandler tblImages.RowChanged, New DataRowChangeEventHandler(AddressOf tblImages_changed)
daImages.Update(dsMAP, "BMPImages")
这是RowChanged Handler ::
Private Sub tblImages_changed(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs)
'Increment progress bar
SavingImageObj.ThreadTask()
If SkipFraming = True Then
' Here I should do something to stop the daImages.Update
' from continuing to execute
End If
End Sub
我设置了一个标志[ SkipFraming ],表示我应该在点击取消按钮时停止操作。
答案 0 :(得分:0)
好的,我找到了解决问题的方法:
我应该为dataAdapter.RowUdpated
而不是dataTable.RowChanged
创建一个事件处理程序,以便能够停止更新[这只是一个状态更新= D( args.Status = UpdateStatus.SkipAllRemainingRows 强>)]
以下是代码示例:
' Reset Updating progress counter
UpdatingProgressCounter = 0
'Open connection to update database
objConn.Open()
'Update database
Log("@@@@@ Saving BMPImages paths to the database @@@@@")
objCommandBuilder = New SqlCommandBuilder(daImages)
SavingImageObj.MyProgressBar.Maximum = tblImages.GetChanges.Rows.Count
AddHandler daImages.RowUpdated, New SqlRowUpdatedEventHandler(AddressOf daImages_updated)
Log("========== Total Rows to be updated = [" & tblImages.GetChanges.Rows.Count & "] ==========")
daImages.Update(dsMAP, "BMPImages")
'Close database connection
objConn.Close()
Private Sub daImages_updated(ByVal sender As Object, ByVal args As System.Data.SqlClient.SqlRowUpdatedEventArgs)
'Increment progress bar
SavingImageObj.ThreadTask()
' Increment progress counter
UpdatingProgressCounter += 1
'Give control on UI
If UpdatingProgressCounter Mod 10 = 0 Then
Application.DoEvents()
SkipFraming = True 'For Testing
End If
' Skip updating remaining rows in case of cancelling the framing
If SkipFraming = True Then
Log("@@@@@@@@@@@@@@@@@ Skipping remaining rows @@@@@@@@@@@@@@@@@")
args.Status = UpdateStatus.SkipAllRemainingRows
End If
End Sub
我希望能帮助有类似情况的人。