How to compare SQL timestamp in .NET?

I have mapped Entity framework entities. Each table in SQL Server 2008 contains Timestamp column which is mapped as byte array. The length of array is always 8.

Now I need to compare timestamp values in .NET. I come with two solutions but I don’t know which one is better?

  • Add Column to Table with Constraints
  • select * from where table=Today's Date in SQL Server Management Studio
  • SQL Server search for a column by name
  • How to change the table name in visual studio 2013 in design mode?
  • Updating variable table from #temp table - expression of non-boolean type specified in a context where a condition is expected
  • Using multiple Databases with single DbContext and Entites and generating Conn String at runtime
    • Compare it as arrays. When first pair of bytes is different return false.
    • Convert byte array to long, compare longs.

    Which solution is better? Or is there any other solution?

  • How to use a single SqlTransaction for multiple SqlConnections in .NET?
  • how to use sql server time data type in .net application?
  • C# Equivalent of SQL Server DataTypes
  • Deploying SQL CLR Project fails when creating assembly on database
  • Is it better to save upload file into SQL Server?
  • How do you effectively model inheritance in a database?
  • 2 Solutions collect form web for “How to compare SQL timestamp in .NET?”

    We do it by comparing them as byte arrays. Works fine for us.

    MS SQL Server’s timestamp data type is semantically equivalent to binary(8) (if non-nullable) or varbinary(8) (if nullable). Ergo, compare them as arrays of bytes.

    Not to mention there’s overhead involved in converting to long. You could write some unsafe code to get the address of the byte arrays, cast those to long pointers and dereference them into longs, BUT to do that safely means pinning them in memory and a raft of ugly code to do something essentially simple (and probably no faster than using BitConverter).

    The fastest way to do it, if performance is really that critical, the fastest way would be to do the comparison using the standard C library’s memcmp() function via P/Invoke:

    using System;
    using System.Runtime.InteropServices;
    namespace TestDrive
        class Program
            static void Main()
                byte[] a = { 1,2,3,4,5,6,7,8} ;
                byte[] b = { 1,2,3,4,5,0,7,8} ;
                byte[] c = { 1,2,3,4,5,6,7,8} ;
                bool isMatch ;
                isMatch = TimestampCompare( a , b ) ; // returns false
                isMatch = TimestampCompare( a , c ) ; // returns true
                return ;
            [DllImport("msvcrt.dll", CallingConvention=CallingConvention.Cdecl)]
            static extern int memcmp(byte[] x , byte[] y , UIntPtr count ) ;
            static unsafe bool TimestampCompare( byte[] x , byte[] y )
                const int LEN = 8 ;
                UIntPtr   cnt = new UIntPtr( (uint) LEN ) ;
                // check for reference equality
                if ( x == y ) return true ;
                if ( x == null || x.Length != LEN || y == null || y.Length != LEN )
                    throw new ArgumentException() ;
                return ( memcmp(  x ,  y , cnt ) == 0 ? true : false ) ;
    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.