在 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)
关于c# - C# 和 SQL Server 中 int 到 guid 的不同转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19656213/