Public Static classGuidutil {Private Static ReadOnly LongEpochmilliseconds =NewDateTime (1970,1,1,0,0,0, DATETIMEKIND.UTC). Ticks/10000L; /// <summary> ///creates a sequential GUID according to SQL Server ' s ordering rules. /// </summary> Public StaticGuid newsequentialid () {//This code is not reviewed to guarantee uniqueness under most conditions, nor completely optimize for avoiding //page splits in SQL Server when doing inserts from the multiple hosts, so does not re-use in production systems. varGuidbytes =Guid.NewGuid (). Tobytearray (); //get the milliseconds since Jan 1 1970 byte[] sequential = Bitconverter.getbytes ((DateTime.Now.Ticks/10000L) -epochmilliseconds); //Discard the 2 most significant bytes, as we have care about the milliseconds increasing, but the highest ones //should is 0 for several thousand years to come (non-issue). if(Bitconverter.islittleendian) {guidbytes[Ten] = sequential[5]; guidbytes[ One] = sequential[4]; guidbytes[ A] = sequential[3]; guidbytes[ -] = sequential[2]; guidbytes[ -] = sequential[1]; guidbytes[ the] = sequential[0]; } Else{buffer.blockcopy (sequential,2, Guidbytes,Ten,6); } return NewGuid (guidbytes); } }
Create order GUID based on SQL Server collation