Results 1 to 1 of 1

Thread: Using Transactions on a Datatable [VB.NET]

  1. #1
    Join Date
    Nov 2004
    Posts
    41
    Rep Power
    0

    Default [HOW TO] Using Transactions on a Datatable [VB.NET]

    Code:
    Public Sub Dwayne_Walters(ByVal dt As DataTable)
                Dim cb as sqlclient.sqlcommanbuilder
                Dim da as sqlclient.sqlDataAdapter
                Dim dtA As New DataTable 'Holds rows Added
                Dim dtD As New DataTable 'Holds rows Deleted
    Dim dtM As New DataTable 'Holds rows Modified
                Dim drw As DataRow
                 Try
                    'This module does not care if u DELETE,INSERT or Update
                    'Because of the Architecture of the DataTable
                    'Which interns simplify the code and makes it more readable
                    'now we get the type of changes made to this DataTable
                    dtA = dt.GetChanges(DataRowState.Added) 
                    dtD = dt.GetChanges(DataRowState.Deleted)
                    dtM = dt.GetChanges(DataRowState.Modified)
    
                    If Not dtA Is Nothing Then 'Rows Inserted 
                        For Each drw In dtA.Rows
                            'Do whatever operations u need to do on these rows
                             Next
                    End If
                    If Not dtD Is Nothing Then 'Rows Deleted
      'cannot loop on deleted rows, can only get count             
     End If
    
                    If Not dtM Is Nothing Then 'Rows Modified
                        For Each drw In dtM.Rows
                              'Do whatever operations u need to do on these rows
                        Next
                    End If
    
     da = New SqlDataAdapter("select [columns] from [table]", [connection])
                cb = New SqlCommandBuilder(da) 'Auto create commands
                da.InsertCommand = cb.GetInsertCommand 'Assign them            
               da.DeleteCommand = cb.GetDeleteCommand
                da.UpdateCommand = cb.GetUpdateCommand
    
    
                    '**************
                    'Begin Transaction*
                    '**************             
                   t = cnn.BeginTransaction(IsolationLevel.Serializable)
                    With da
    'Set each Transaction property for each command object of the DataAdapter                    
                        .InsertCommand.Transaction = t
                        .UpdateCommand.Transaction = t
                        .DeleteCommand.Transaction = t
                    End With
                   da.Update(dt) 
                    '*******************
                    'Commit Transaction*
                    '*******************             
       t.Commit()
                Catch ex As Exception
                    '*********
                    'RollBack*
                    '*********
                     If Not t Is Nothing Then 'this means the transaction has begun
                        t.Rollback()
                    End If
                 Finally
                    'Clean up
                    dtA = Nothing
                    dtD = Nothing
                    dtM = Nothing
                    drw = Nothing
                    da = Nothing
                    cb=nothing
                    t = Nothing
                 End Try
    
            End Sub
    Last edited by Bredz; Mar 30, 2005 at 10:55 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •