You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将ulong类型转换为SQL Server rowversion对应的8字节byte数组

ulong转SQL Server RowVersion兼容byte[8]实现

由于SQL Server的rowversion是大端序存储,转换时需要严格保持字节顺序和原有转换逻辑对齐,推荐使用和之前转换逻辑完全对称的实现:

static byte[] UInt64ToBigEndianBytes(ulong value)
{
    byte[] result = new byte[8];
    result[0] = (byte)(value >> 56);
    result[1] = (byte)(value >> 48);
    result[2] = (byte)(value >> 40);
    result[3] = (byte)(value >> 32);
    result[4] = (byte)(value >> 24);
    result[5] = (byte)(value >> 16);
    result[6] = (byte)(value >> 8);
    result[7] = (byte)value;
    return result;
}

该实现不依赖运行环境的字节序,输出的字节数组和原始rowversion完全一致,可直接作为查询参数使用。

注意事项

请勿直接使用BitConverter.GetBytes(ulong)处理,Windows等多数常用系统为小端字节序,该方法输出的字节顺序会和要求的大端序相反,直接传入会导致查询匹配失败。如果需要基于BitConverter实现,可参考如下写法:

static byte[] UInt64ToBigEndianBytes(ulong value)
{
    byte[] bytes = BitConverter.GetBytes(value);
    if (BitConverter.IsLittleEndian)
        Array.Reverse(bytes);
    return bytes;
}

内容的提问来源于stack exchange,提问作者user1814688

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.21 03:06:03