// 另外,我必须检索数据库生成的 GUID,而不是在客户端创建它。//
(((请注意,我想将此作为评论,但“代码”对于简单评论来说太大了。但这〜可能是一个答案。)))
您可以创建客户端(主要是)顺序 guid ..... 作为 C# 开发人员而不是 DBA,我喜欢在数据库之外创建所有关系,然后将所有内容作为 Xml 发送,让 TSQL 分解它,然后在一个 db hit 中插入我需要的所有内容(我猜偶尔也会删除)。但这种方法也有不足之处。我认为 int 和 bigint 通常会“赢得”表演比赛......但是........大多数时候............在客户端“顺序”使用 guid足以满足我的需求。特别是,正如你所建议的......你必须在某个时候移动这些数据。使用 IDENTITY() 让 PK/FK 的“阵容”是可行的,但使用 GUID(恕我直言)更容易。现在,如果我正在编写 T~ick3t M@ster 或 Am@zing 在线零售网站,我不会选择 GUID。
祝你好运。
using System;
using System.Runtime.InteropServices;
namespace MyCompany.MyTechnology.Framework.CrossDomain.GuidExtend
{
public static class Guid
{
/*
Original Reference for Code:
http://www.pinvoke.net/default.aspx/rpcrt4/UuidCreateSequential.html
*/
[DllImport("rpcrt4.dll", SetLastError = true)]
static extern int UuidCreateSequential(out System.Guid guid);
public static System.Guid NewGuid()
{
return CreateSequentialUuid();
}
public static System.Guid CreateSequentialUuid()
{
const int RPC_S_OK = 0;
System.Guid g;
int hr = UuidCreateSequential(out g);
if (hr != RPC_S_OK)
throw new ApplicationException("UuidCreateSequential failed: " + hr);
return g;
}
/*
Text From URL above:
UuidCreateSequential (rpcrt4)
Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than rpcrt4, prefix the name with the module name and a period.
. Summary
Creates a new UUID
C# Signature:
[DllImport("rpcrt4.dll", SetLastError=true)]
static extern int UuidCreateSequential(out Guid guid);
VB Signature:
Declare Function UuidCreateSequential Lib "rpcrt4.dll" (ByRef id As Guid) As Integer
User-Defined Types:
None.
Notes:
Microsoft changed the UuidCreate function so it no longer uses the machine's MAC address as part of the UUID. Since CoCreateGuid calls UuidCreate to get its GUID, its output also changed. If you still like the GUIDs to be generated in sequential order (helpful for keeping a related group of GUIDs together in the system registry), you can use the UuidCreateSequential function.
CoCreateGuid generates random-looking GUIDs like these:
92E60A8A-2A99-4F53-9A71-AC69BD7E4D75
BB88FD63-DAC2-4B15-8ADF-1D502E64B92F
28F8800C-C804-4F0F-B6F1-24BFC4D4EE80
EBD133A6-6CF3-4ADA-B723-A8177B70D268
B10A35C0-F012-4EC1-9D24-3CC91D2B7122
UuidCreateSequential generates sequential GUIDs like these:
19F287B4-8830-11D9-8BFC-000CF1ADC5B7
19F287B5-8830-11D9-8BFC-000CF1ADC5B7
19F287B6-8830-11D9-8BFC-000CF1ADC5B7
19F287B7-8830-11D9-8BFC-000CF1ADC5B7
19F287B8-8830-11D9-8BFC-000CF1ADC5B7
Here is a summary of the differences in the output of UuidCreateSequential:
The last six bytes reveal your MAC address
Several GUIDs generated in a row are sequential
Tips & Tricks:
Please add some!
Sample Code in C#:
static Guid UuidCreateSequential()
{
const int RPC_S_OK = 0;
Guid g;
int hr = UuidCreateSequential(out g);
if (hr != RPC_S_OK)
throw new ApplicationException
("UuidCreateSequential failed: " + hr);
return g;
}
Sample Code in VB:
Sub Main()
Dim myId As Guid
Dim code As Integer
code = UuidCreateSequential(myId)
If code <> 0 Then
Console.WriteLine("UuidCreateSequential failed: {0}", code)
Else
Console.WriteLine(myId)
End If
End Sub
*/
}
}