将SQL Server rowversion转换为long或ulong? [英] converting sql server rowversion to long or ulong?
问题描述
rowversion(时间戳)数据类型的正确类型是什么?
What is the proper type for the rowversion (timestamp) data type?
我知道它是8个字节,但是我无法在MSDN中找到一个链接,该链接表明它是带符号的还是无符号的长.
I know it is 8 bytes but i cannot find a link in MSDN which tell if it is a signed or unsigned long.
我应该使用哪个代码,这有关系吗?
which code should I use, does it even matter?
byte[] SqlTimeStamp;
long longConversion;
longConversion = BitConverter.ToInt64(SqlTimeStamp,0);
TimeStamp = BitConverter.GetBytes(longConversion);
ulong ulongConversion;
ulongConversion = BitConverter.ToUInt64(SqlTimeStamp,0);
TimeStamp = BitConverter.GetBytes(ulongConversion);
推荐答案
简短的回答:没关系,但是我选择UInt64
.
Short answer: it doesn't matter but I'd choose UInt64
.
详细信息:从语义上讲,它等效于binary(8)
,因此严格来说,它既不是UInt64
也不是Int64
,而只是一个字节块(因此应该对其进行管理).那就是说我选择UInt64
是因为它是一个递增数字来保存行版本,然后(从逻辑的角度来看)0xFFFFFFFFFFFFFFFF
应该大于0
,而对于Int64
(因为将64位设置为1会给出-1
,并且小于0
).
Details: semantically it's equivalent to binary(8)
so, strictly speaking, it's neither UInt64
nor Int64
but just a chunk of bytes (and in that way it should be managed). That said I'd choose UInt64
because it's an incrementing number to hold row version then (from a logic point of view) 0xFFFFFFFFFFFFFFFF
should be greater than 0
and it's not true for Int64
(because 64 bits set to 1 give -1
and it's less than 0
).
编辑:请注意,由于仅在SQL Server设计者圈子中才知道的原因,ROWVERSION
是大尾数法(而-显然-bigint
不是),那么您首先需要反向字节,请参见此答案以实现良好的实现.
Edit: note that, for reasons known only in the innest SQL Server designers circle, ROWVERSION
is big-endian (while - obviously - bigint
is not) then you first need to reverse bytes, see this answer for a nice implementation.
这篇关于将SQL Server rowversion转换为long或ulong?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!