none
在一個 DataSet 中有多表格的「並行違規」要如何 try..catch..End Try 比較好 RRS feed

  • 問題

  • 由前面爬文知道單一表格的「並行違規」,比較簡單的方法可以這樣做

            Try
                Me.myTableAdapter.Update(Me.myDataSet.myDataTable)
                Me.myDataSet.myDataTable.AcceptChanges()
            Catch dbex As Data.DBConcurrencyException
                ''並行違規的處理(強制更新)
                Try
                    Dim nTB As New myDataSet.myDataTable
                    Me.myTableAdapter.myDataTable(nTB)
                    Me.myDataSet.myDataTable.Merge(nTB, True)
                    If Me.myTableAdapter.Update(Me.myDataSet.myDataTable) > 0 Then
                        Me.myDataSet.myDataTable.AcceptChanges()
                    Else
                        'Return False
                        MessageBox.Show("同步資料時發生錯誤")
                    End If
                    nTB.Dispose()
                    nTB = Nothing
                Catch ex As Exception
                    'Return False
                    MessageBox.Show("同步資料時發生錯誤" & ex.Message)
                End Try
            Catch ex As Exception
                ''
                MessageBox.Show("同步資料時發生錯誤" & ex.Message)
            End Try

     

    那如果為 Master / Detail 表格的狀況下,當發生「並行違規」時該如何做才不會發生寫入部份資料
    的狀況。也就是說 Try...Catch...End Try 該如何包會比較好。底下是儲存時的部份程式碼


                Dim deletedChildRecords As dstTA2140.TA327DataTable = _
                       CType(objDstTA2140.TA327.GetChanges(Data.DataRowState.Deleted), dstTA2140.TA327DataTable)

                Dim newChildRecords As dstTA2140.TA327DataTable = _
                       CType(objDstTA2140.TA327.GetChanges(Data.DataRowState.Added), dstTA2140.TA327DataTable)

                Dim modifiedChildRecords As dstTA2140.TA327DataTable = _
                       CType(objDstTA2140.TA327.GetChanges(Data.DataRowState.Modified), dstTA2140.TA327DataTable)

                Try
                    ' Step 1.
                    If deletedChildRecords IsNot Nothing Then
                        intRowsCountD += Me.tadTA327.Update(deletedChildRecords)
                    End If

                    ' Step 2.
                    intRowsCountH = Me.tadTA326.Update(objDstTA2140.TA326)

                    ' Step 3.
                    If newChildRecords IsNot Nothing Then
                        intRowsCountD += Me.tadTA327.Update(newChildRecords)
                    End If

                    ' Step 3.
                    If modifiedChildRecords IsNot Nothing Then
                        intRowsCountD += Me.tadTA327.Update(modifiedChildRecords)
                    End If

                    objDstTA2140.AcceptChanges()
                Catch dbex As DBConcurrencyException
                    ' 並行違規處理
                  
                Catch ex As Exception
                    MessageBox.Show(ex.Message.ToString, "儲存錯誤!", MessageBoxButtons.OK,  

         System.Windows.Forms.MessageBoxIcon.Warning)
                Finally
                    If deletedChildRecords IsNot Nothing Or newChildRecords IsNot Nothing Or modifiedChildRecords IsNot

         Nothing Then
                        Me.tadTA327.Dispose()
                    End If
                End Try

     

    2008年8月28日 上午 07:38