在 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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)