从int转换为uniqueidentifier [英] convert from int to uniqueidentifier
问题描述
如果我有一个sql数据库,其中包含大量带有int主键的数据,我想将它们转换为uniqueidentifier并保持关系
我应该怎么做
我使用uniqueidentifier密钥的原因是:
i与许多具有相同数据库模式的服务器一起工作,并且确保永远不会发生重复密钥的唯一方法是使用uniqueidentifier
if i have an sql database that contains a lot of data with int primary keys and i want to convert them into uniqueidentifier with keeping the relationships
what should i do
my reason for using uniqueidentifier keys is :
i work with many servers with the same database schema and the only way to ensure that duplicate keys will never occurs is using uniqueidentifier
推荐答案
如果你已经有了一个有效的数据库和应用程序,那么这是一件非常困难的事情,需要对它们进行部分重新设计,这最多可能是非常重要的。
<祝你好运!祝你好运!
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.
这篇关于从int转换为uniqueidentifier的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!