0

I have a database with several fields of various types (basically String, Integer and Decimal). I'd like to create a control field to store something like a checksum in order to detect simple value changes from any among those fields.

I first thought of collecting .GetHashCode from each field value just before calling INSERT INTO against the database, and performing some arithmetical operation complex enough to be difficult form someone to manually replicate, and putting this result as value for the control field:

Function ControlFieldValue(ParamArray values() As IEnumerable(Of Object)) As Long
    Dim retval As Long
    For Each v In values
        If retval = 0 Then
            retval = v.GetHashCode
        Else
            retval = retval Mod v.GetHashCode
        End If
    Next
    Return retval
End Function

But I don't know how effective this would be. Is there some simple implementation in VB.NET for such purpose? Thank you very much.

VBobCat
  • 2,527
  • 4
  • 29
  • 56
  • Don't persist the result of `GetHashCode`. Microsoft does not guarantee it to be stable across versions nor across platforms. Differences in both areas have occurred in the past: the calculated hash has been different on different platforms (x32 vs x64), and the calculated hash has changed between versions of the .NET framework. – Craig Mar 24 '17 at 20:10
  • Depending on your backing DB, you may be able to log changes. See [Log record changes in SQL server in an audit table](http://stackoverflow.com/questions/19737723/log-record-changes-in-sql-server-in-an-audit-table) for one possible solution. – TnTinMn Mar 24 '17 at 21:08
  • @Craig, thanks, I'll discard that approach. – VBobCat Mar 24 '17 at 21:30
  • @TnTinMn, no, it's Access in some cases, SQLite in others. – VBobCat Mar 24 '17 at 21:30

0 Answers0