如何使用c#生成唯一的8位数字 [英] How to generate a unique 8 digit number using c#
问题描述
大家好吧
我想设计一个带有两个文本框的winform,第一个框是pincode,pincode txtbox是readonly所以无法编辑,另一个框是fullname。我希望在加载表单时,pincode文本框会自动生成8位唯一引脚,该引脚将被插入表中的主键列,因此没有两个密码应该相同。如果我再次加载表单,则生成另一个引脚,依此类推,等等。
提前谢谢
试试 Guid.NewGuid方法 [ ^ ]
通常8位数不是确保您的ID是唯一的。我建议您使用GUID,因为它们是标准的并提供足够的唯一性。有关详细信息,请参阅此处:
http://en.wikipedia.org/wiki/Globally_unique_identifier [ ^ ]
并参考回答Prasad如何在C#中创建ID。
这里还有一篇CP文章讨论这个主题并为你提供更多选项:
在.Net中生成唯一密钥 [ ^ ]
< blockquote>
检查这个
随机rand = 新随机( 100 );
int ccc = rand.Next( 000000000 , 999999999 );
http://www.c-sharpcorner.com/UploadFile/mahesh/RandomNumber11232005010428AM/RandomNumber.aspx [ ^ ]
http: //www.dotnetperls.com/random [ ^ ]
最好的问候
M.Mitwalli
Hi everybody
i want to design a winform with two textboxes the first box is pincode, the pincode txtbox is readonly so cannot be edited, the other box is fullname. i want that when the form is loaded the pincode textboxes automatically generate 8 digit unique pin,that will be inserted into the primary key column in the table so no two pincode number should be the same. if i load the form again another pin is generated and so on and on and on, .
Thanks in advance
Try Guid.NewGuid Method[^]
Normally 8 digits are not to be sure that your ID is unique. I suggest you to use GUID's as they are standard and provide sufficient uniqueness. See here for more information:
http://en.wikipedia.org/wiki/Globally_unique_identifier[^]
and refer the answer by Prasad for how to create the ID's in C#.
Here is also a CP article discussing the topic and providing some more options for you:
Generating Unique Keys in .Net[^]
Hi ,
Check this
Random rand = new Random(100); int ccc= rand.Next(000000000, 999999999);
http://www.c-sharpcorner.com/UploadFile/mahesh/RandomNumber11232005010428AM/RandomNumber.aspx[^]
http://www.dotnetperls.com/random[^]
Best Regards
M.Mitwalli
这篇关于如何使用c#生成唯一的8位数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!