GUID as the database primary key because of its disorder so performance is not good, SQL server has a function newsequentialid can generate an ordered GUID, because in the program needs to use, in C # implementation, the resulting GUID format basic and SQL Server is consistent.
The program code references the implementation of UuidCreateSequential in Rpcrt4.dll.
1 Public classGuidhelper2 {3 Private Static BOOLinitialised;4 Private Static intcount;5 6 Private Static LongTime ;7 Private Static LongTimelast;8 Private Static ushortsequence;9 Ten Private Static byte[] address;//Network card mac One Private Static ReadOnly ObjectLocker =New Object(); A Public StaticGuid newsequentialid () - { - Lock(Locker) the { - if(!initialised) - { -Timelast =uuidgetsystemtime (); +Count =Ticks_per_clock_tick; -Random Rand =NewRandom (1); +Sequence = (ushort) ((Rand. Next (1,32767) &0xFF) <<8) + Rand. Next (1,32767) &0xFF); ASequence &=0x1fff; atAddress =getaddressbytes (); -initialised =true; - } - while(true) - { -Time =uuidgetsystemtime (); in if(Time >timelast) - { toCount =0; + Break; - } the if(Time <timelast) * { $Sequence = (ushort) ((Sequence +1) &0x1fff);Panax NotoginsengCount =0; - Break; the } + if(Count <Ticks_per_clock_tick) A { thecount++; + Break; - } $ } $ -Timelast =Time ; -Time + =count; the - byte[] Guidarray =New byte[ -];Wuyi the UINTData1 = (UINT) (Time &0xFFFFFFFF); - ushortData2 = (ushort) (Time >> +) &0xFFFF); Wu ushortData3 = (ushort) (Time >> -) &0x0fff); - /*This is a version 1 UUID*/ AboutData3 |= (1<< A); $ -guidarray[3] = (byte) data1; -guidarray[2] = (byte) (Data1 >>8); -guidarray[1] = (byte) (Data1 >> -); Aguidarray[0] = (byte) (Data1 >> -); +guidarray[5] = (byte) data2; theguidarray[4] = (byte) (Data2 >>8); -guidarray[7] = (byte) data3; $guidarray[6] = (byte) (Data3 >>8); theguidarray[8] = (byte) (Sequence &0xFF); theguidarray[9] = (byte) ((Sequence &0x3f00) >>8); theguidarray[9] |=0x80; theArray.copy (Address,0, Guidarray,Ten,6); - in return NewGuid (guidarray); the}//Locker the}//uuidcreatesequential About the Private Static ReadOnly intTicks_per_clock_tick = +; the Private Static ReadOnly intSecsperday =86400; the Private Static ReadOnly intTickspersec =10000000; + Private Static ReadOnly Longsecs_15_oct_1582_to_1601 = ( -+ -+ to+365* -+5) *Secsperday; - Private Static ReadOnly Longticks_15_oct_1582_to_1601 = secs_15_oct_1582_to_1601 *tickspersec; the Private Static Longuuidgetsystemtime ()Bayi { theDateTime dt =DateTime.Now; the varFT =dt. Tofiletime (); -FT + =ticks_15_oct_1582_to_1601; - returnft; the } the the Private Static byte[] getaddressbytes () the { - byte[] bytes; thenetworkinterface[] nic =networkinterface.getallnetworkinterfaces (); the if(nic = =NULL|| Nic. Length <1) the {94bytes =New byte[6]; thebytes[0] =0x01; the returnbytes; the }98bytes = nic[0]. Getphysicaladdress (). Getaddressbytes (); About returnbytes; - }101}
Code write rough, but the function has been achieved, the need for friends can optimize their own!
C # implements ordered GUID generation in SQL Server database (NEWSEQUENTIALID)