且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从int转换为uniqueidentifier

更新时间:2023-01-17 10:38:28

如果你已经有了一个有效的数据库和应用程序,那么这是一件非常困难的事情,需要对它们进行部分重新设计,这最多可能是非常重要的。

If you already have a working database and application then this is a very hard thing to do and requires a partial redesign of both, which can be nontrivial at best.

Good luck!


最简单的方法是:



The simplest way is:

int k = 0x12345678 ;
System.Guid g = new System.Guid ( k , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) ; 

{12345678-0000-0000-0000-000000000000}





但我宁愿这样做方式:





But I'd rather do it this way:

int k = 0x12345678 ;
System.Guid g = new System.Guid ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 
(byte) ( k >> 24 & 0xFF ) , (byte) ( k >> 16 & 0xFF ) , (byte) ( k >> 8 & 0xFF ) , (byte) ( k & 0xFF ) ) ;

{00000000-0000-0000-0000-000012345678}





你可以继续使用整数本地数据库并仅为中间数据库使用GUID - 并使用GUID的未使用部分来指示记录来自哪个站点。



You can continue to use integers on the local database and use GUIDs only for the central database -- and use an unused section of the GUID to indicate which site the record came from.


有一个指导构造函数重载 [ ^ ],它允许您指定guid的组成部分。



There is a Guid constructor overload[^] which allows you to specify the constituent part of the guid.

int pk = 123456789;
short system = 5;
var guid = new Guid(pk, system, 0, new byte[] {0, 0, 0, 0, 0, 0, 0, 0});





但这是一种矫枉过正的因为Guid是16个字节,你真的需要5或6.也许你应该创建自己的结构。



But it's an overkill becuase Guid is 16 bytes and you really need just 5 or 6. Maybe you should create your own structure.