How to read timestamp type's data from sql server using C#?
Asked Answered
C

6

13

I get the result in .NET like this:

var lastRowVersion = SqlHelper.ExecuteScalar(connStr, CommandType.Text, "select
top 1 rowversion from dbo.sdb_x_orginfo order by rowversion desc");

The result is a byte array [0]= 0,[1]=0,[2]=0,[3]=0,[4]=0,[5]=0,[6]=30,[7]=138, but the result in SQL Server is 0x0000000000001E8A.

How can I get the value "0x0000000000001E8A" in .NET?

Caterpillar answered 8/11, 2011 at 6:23 Comment(0)
C
5

If you just want to convert byte[] to a System.Int64 (aka long) then use BitConverter.ToInt64:

SqlBinary binary = /* ... */;
long value = BitConverter.ToInt64(binary.Value, 0); // 0 is the start index in the byte array

To display it as a hex string, you can use the X format specifier, e.g.:

Console.WriteLine("Value is 0x{0:X}.", value);
Carmine answered 8/11, 2011 at 6:35 Comment(1)
See the other answer - SqlBinary.Value has the opposite endianness, so you most likely want to reverse the bytes.Barthelemy
N
26

I found that the byte[] returned from sql server had the wrong Endian-ness and hence the conversion to long (Int64) did not work correctly. I solved the issue by calling Reverse on the array before passing it into BitConverter:

byte[] byteArray = {0, 0, 0, 0, 0, 0, 0, 8};

var value = BitConverter.ToUInt64(byteArray.Reverse().ToArray(), 0);

Also, I thought it better to convert to UInt64.

Nanette answered 24/1, 2013 at 23:56 Comment(0)
C
5

If you just want to convert byte[] to a System.Int64 (aka long) then use BitConverter.ToInt64:

SqlBinary binary = /* ... */;
long value = BitConverter.ToInt64(binary.Value, 0); // 0 is the start index in the byte array

To display it as a hex string, you can use the X format specifier, e.g.:

Console.WriteLine("Value is 0x{0:X}.", value);
Carmine answered 8/11, 2011 at 6:35 Comment(1)
See the other answer - SqlBinary.Value has the opposite endianness, so you most likely want to reverse the bytes.Barthelemy
E
2

Here's an one-liner:

var byteArray = new byte[] { 0, 0, 0, 0, 0, 0, 30, 138 };

var rowVersion = "0x" + string.Concat(Array.ConvertAll(byteArray, x => x.ToString("X2")));

The result is:

"0x0000000000001E8A"

Just be aware that there are other options that perform better.

Executioner answered 12/7, 2017 at 16:31 Comment(0)
F
0

You can use following query which will return bigint instead of returning hex.

select top 1 cast(rowversion as bigint) rowversion from dbo.sdb_x_orginfo order by rowversion desc

Falter answered 20/4, 2014 at 18:37 Comment(1)
Be aware that bigint is a 64bit signed integer, whereas rowversion is a 64bit unsigned integer. Given a large enough rowversion value, casting to bigint will give unexpected results.Jeremyjerez
L
0

Same as "Ashish Singh" I returned to c# after converting to bigint on DB and used Int64 on the code side. Converting to long/Int64/UInt64 in c#/VB gave wrong results.(even after reversing the array because of the little endian issue.)

Pay attention on the DB side that RowVersion timestamp has MIN_ACTIVE_ROWVERSION() which indicates the last commited timestamp

Ley answered 3/3, 2015 at 13:14 Comment(0)
P
0

Works fine for me

  var versionString = new StringBuilder();
  versionString.Append("0x");
  for (var i = 0; i < binary.Count(); i++)
  {
      versionString.Append(string.Format("{0:X}", binary[i]));
  }
  versionString.ToString(); // result
Pentastyle answered 29/2, 2016 at 10:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.