在C#和SQL Server中不同地将int转换为guid

Mla*_*vić 12 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 ).ToString() );
// writes 000003e8-0000-0000-0000-000000000000
Run Code Online (Sandbox Code Playgroud)

在SQL Server中我使用

select cast(cast(1000 as varbinary(16)) as uniqueidentifier)
-- writes E8030000-0000-0000-0000-000000000000
Run Code Online (Sandbox Code Playgroud)

他们为什么表现不同?

Kla*_*ark 16

这是因为sql server和.net以不同的格式存储int.这样就可以了:

select cast(CONVERT(BINARY(16), REVERSE(CONVERT(BINARY(16), 1000))) as uniqueidentifier)
Run Code Online (Sandbox Code Playgroud)