为int的不同铸件在C#和SQL服务器GUID [英] Different casting of int to guid in C# and SQL Server
本文介绍了为int的不同铸件在C#和SQL服务器GUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在转换INT在C#和SQL Server,我得到不同的值的GUID。
When converting int to guid in C# and SQL Server I get different values.
在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我用
In SQL Server I use
select cast(cast(1000 as varbinary(16)) as uniqueidentifier)
-- writes E8030000-0000-0000-0000-000000000000
为什么他们会表现不同?
Why would they behave differently?
推荐答案
这是因为SQL Server和.NET存储int类型不同的格式。
这样做的伎俩:
This happens because sql server and .net store int in different format. This will do the trick:
select cast(CONVERT(BINARY(16), REVERSE(CONVERT(BINARY(16), 1000))) as uniqueidentifier)
这篇关于为int的不同铸件在C#和SQL服务器GUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文