我复制了以下从http://www.dbdelta.com/improving-uniqueidentifier-performance/截取的代码,以创建连续的Guids:
private static Guid NewSequentialGuid()
{
const int S_OK = 0;
const int RPC_S_UUID_LOCAL_ONLY = 1824;
Guid oldGuid = Guid.Empty;
int result = UuidCreateSequential(ref oldGuid);
if (result != S_OK && result != RPC_S_UUID_LOCAL_ONLY)
{
throw new ExternalException("UuidCreateSequential call failed", result);
}
byte[] oldGuidBytes = oldGuid.ToByteArray();
byte[] newGuidBytes = new byte[16];
oldGuidBytes.CopyTo(newGuidBytes, 0);
// swap low timestamp bytes (0-3)
newGuidBytes[0] = oldGuidBytes[3];
newGuidBytes[1] = oldGuidBytes[2];
newGuidBytes[2] = oldGuidBytes[1];
newGuidBytes[3] = oldGuidBytes[0];
// swap middle timestamp bytes (4-5)
newGuidBytes[4] = oldGuidBytes[5];
newGuidBytes[5] = oldGuidBytes[4];
// swap high timestamp bytes (6-7)
newGuidBytes[6] = oldGuidBytes[7];
newGuidBytes[7] = oldGuidBytes[6];
//remaining 8 bytes are unchanged (8-15)
return new Guid(newGuidBytes);
}让我困惑的是&& result != RPC_S_UUID_LOCAL_ONLY检查。我不明白为什么/什么时候我应该或不应该检查这个值。
有人能澄清一下吗?我能不能把这张支票留下,或者我会遇到问题?
发布于 2017-01-19 16:46:24
感谢@Damnien_The_Unbeliever让我看到了显而易见的事情。
https://stackoverflow.com/questions/41737215
复制相似问题