C#生成唯一的ID保存到数据库

直接用.NET Framework 提供的 Guid() 函数:

Guid.NewGuid()是指生成唯一码的规则

System.Guid.NewGuid().ToString()全球唯一标识符 (GUID) 是一个字母数字标识符

System.Guid.NewGuid().ToString(format):生成的ID值的格式:

说明符       返回值的格式  
 
N                  32   位:  

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  
 
D                  由连字符分隔的   32   位数字:  

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx  
 
B                  括在大括号中、由连字符分隔的   32   位数字:  

{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}  
 
P                  括在圆括号中、由连字符分隔的   32   位数字:  

(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)  

Guid guid = Guid.NewGuid();
string id = guid.ToString("N");

保存到数据库中后,就是一串32位的字符串

原文地址:https://www.cnblogs.com/Donnnnnn/p/5996031.html