C#和SQL Server中int到guid的不同转换

C#和SQL Server中int到guid的不同转换,c#,sql-server,int,guid,uniqueidentifier,C#,Sql Server,Int,Guid,Uniqueidentifier,在C#和SQL Server中将int转换为guid时,我得到了不同的值 在C#中,我使用这种方法 public static Guid Int2Guid( int value ) { byte[] bytes = new byte[16]; BitConverter.GetBytes( value ).CopyTo( bytes, 0 ); return new Guid( bytes ); } Console.Write( Int2Guid( 1000 ).ToSt

在C#和SQL Server中将int转换为guid时,我得到了不同的值

在C#中,我使用这种方法

public static Guid Int2Guid( int value )
{
    byte[] bytes = new byte[16];
    BitConverter.GetBytes( value ).CopyTo( bytes, 0 );
    return new Guid( bytes );
}

Console.Write( Int2Guid( 1000 ).ToString() );
// writes 000003e8-0000-0000-0000-000000000000
在SQL Server中,我使用

select cast(cast(1000 as varbinary(16)) as uniqueidentifier)
-- writes E8030000-0000-0000-0000-000000000000

为什么它们的行为会不同?

之所以会出现这种情况,是因为sql server和.net以不同的格式存储int。 这将实现以下目的:

select cast(CONVERT(BINARY(16), REVERSE(CONVERT(BINARY(16), 1000))) as uniqueidentifier)

对于每个组中的前8个字节,SQL Server中每个组中的字节都是“反向的”。检查文档中是否有
uniqueidentifier

它指出有两种提供值的方法-注意字节的顺序:

字符串格式 “6F9619FF-8B86-D011-B42D-00C04FC964FF”

二进制格式 0xFF19966F868B11D0B42D0C04FC964FF