Tuesday, June 3, 2014

Convert int to guid in C# and SQL Server

C#
public static Guid Int2Guid(int value)

{
    byte[] bytes = new byte[16];
    BitConverter.GetBytes(value).CopyTo(bytes, 0);
    return new Guid(bytes);
}
SQL Server

CAST(CONVERT(BINARY(16), REVERSE(CONVERT(BINARY(16), 13))) AS uniqueidentifier)

No comments:

Post a Comment