Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Audit Trail
Message
De
16/06/2009 14:39:19
 
 
À
16/06/2009 08:45:48
Information générale
Forum:
ASP.NET
Catégorie:
The Mere Mortals .NET Framework
Titre:
Divers
Thread ID:
01406299
Message ID:
01406436
Vues:
62
If you dont have the option of doing it in the Database ( on one of my projects I didnt have the option) I have some code I used in the ABusinessObject.
   Protected Overrides Function HookpreSave(ByVal dt As DataTable) As Boolean
        'if using audit trail pass each data row to the audit trail function.
        If Me.UseAuditTrail = True Then
            Dim dr As DataRow
            For Each dr In dt.Rows
                Me.AuditTrail(dr)
            Next
        End If
        Return True
    End Function
    ''' <summary>
    ''' Creates the audit trail entry of the passed datarow.
    ''' </summary>
    ''' <param name="dr"></param>
    ''' <remarks></remarks>
    Private Sub AuditTrail(ByVal dr As DataRow)
        Try
            'find the "state", adding editing or deleting. 
            Dim strState As String = ""
            Select Case dr.RowState
                Case DataRowState.Added
                    strState = "Added"
                Case DataRowState.Modified
                    strState = "Updated"
                Case DataRowState.Deleted
                    strState = "Deleted"
            End Select
            'generate the PK
            Dim param1 As IDbDataParameter = Me.CreateParameter("@ID", System.Guid.NewGuid())
            'get the table name
            Dim param2 As IDbDataParameter = Me.CreateParameter("@TableName", dr.Table.TableName)
            'get the loggedin user
            My.User.InitializeWithWindowsUser()
            Dim param3 As IDbDataParameter = Me.CreateParameter("@UserID", My.User.Name)
            'get the "state"
            Dim param4 As IDbDataParameter = Me.CreateParameter("@AuditType", strState)
            ' fill in last modified
            Dim param7 As IDbDataParameter = Me.CreateParameter("@LastUpdatedOn", DateTime.Now)
            'gather primary key info
            Dim pkColumns As Array = dr.Table.PrimaryKey
            Dim colPK As DataColumn = pkColumns(0)
            'colPK.ColumnName
            Dim param8 As IDbDataParameter = Me.CreateParameter("@PrimaryKey", dr(colPK.ColumnName))
            Dim fielddata As String = ""
            Dim oldfielddata As String = ""
            Dim col As DataColumn
            'if this row is being added for the first time every field will be new
            'so we will just loop through them all.
            If dr.RowState = DataRowState.Added Then
                For Each col In dr.Table.Columns
                    'make a string containing field name and value of each field
                    fielddata += col.ColumnName.ToString() + "=" + dr(col).ToString() + ";"
                Next
            End If
            'if this row is already in the DB and is being edited we will just record the edited fields.
            If dr.RowState = DataRowState.Modified Then
                'make sure we have the original version before processing this row
                If dr.HasVersion(DataRowVersion.Original) = True Then
                    'loop through all the fields compairing the original value to the current value
                    'if the values dont match then add the field name and value to string
                    For Each col In dr.Table.Columns
                        If dr(col, DataRowVersion.Original).ToString <> dr(col, DataRowVersion.Current).ToString Then
                            'current version
                            fielddata += col.ColumnName.ToString() + "=" + dr(col).ToString() + ";"
                            'original version
                            oldfielddata += col.ColumnName.ToString() + "=" + dr(col, DataRowVersion.Original).ToString() + ";"
                        End If
                    Next
                End If
            End If
            'For deleted rows we grab all the fields and values
            If dr.RowState = DataRowState.Deleted Then
                If dr.HasVersion(DataRowVersion.Original) = True Then
                    For Each col In dr.Table.Columns
                        fielddata += col.ColumnName.ToString() + "= deleted;"
                        oldfielddata += col.ColumnName.ToString() + "=" + dr(col, DataRowVersion.Original).ToString() + ";"
                    Next
                End If
            End If
            If dr.RowState = DataRowState.Deleted Then
                If dr.HasVersion(DataRowVersion.Original) = True Then
                    For Each col In dr.Table.Columns
                        'If dr(col, DataRowVersion.Original).ToString <> dr(col, DataRowVersion.Current).ToString Then
                        fielddata += col.ColumnName.ToString() + "= deleted;"
                        oldfielddata += col.ColumnName.ToString() + "= New row;"
                        'End If
                    Next
                End If
            End If
            'add the field strings that we created above
            Dim param5 As IDbDataParameter = Me.CreateParameter("@BeforeData", oldfielddata)
            Dim param6 As IDbDataParameter = Me.CreateParameter("@AfterData", fielddata)
            'send the insert to the DB
            Dim lcsql As String = "INSERT INTO AuditTrail([ID],[TableName],[UserID],[AuditType]," + _
                           "[BeforeData],[AfterData],[LastUpdatedOn],[PrimaryKey]) VALUES (" + _
                            "@ID,@TableName,@UserID,@AuditType,@BeforeData,@AfterData,@LastUpdatedOn,@PrimaryKey)"
            Me.ExecNonQuery(lcsql, param1, param2, param3, param4, param5, param6, param7, param8)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Audit Trail has caused an error")
        End Try
    End Sub
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform