如何在asp.net C#中为每个用户创建唯一的ID
我为每个配置文件使用了随机数,但它不是唯一的。
Guid.NewGuid()几乎是独一无二的。
Guid id = Guid.NewGuid();
// Output: e62e2706-a8db-4fae-891a-65985fd80351
If you’re using SQL Server (I’m pretty sure something similar exists in Oracle and MySQL but I can’t confirm) you can use identity column with integer value that will automatically generate unique ID for new user.
If you need something different like you shown in one of the comments “INMHPU123333” then you’ll need to create a separate function for this.
Note that this is not really ideal from the performance perspective because it will cause big index fragmentation. Not a huge deal though if you don’t have large number of records.