生成在c#8位uinque ID [英] Generate 8 digit uinque id in c#

查看:416
本文介绍了生成在c#8位uinque ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要生成在c#8位数的唯一ID。在我的网站的用户将注册,我需要在c#code生成一个唯一的ID为他(我不想在DB这个逻辑),插入ID后,我必须将它保存在数据库中。

I need to generate 8 digit unique id in c#. On my site a user will register and I need to generate a unique id for him in c# code(I don't want this logic in DB), after inserting the id I need to save it in database.

编辑:我需要以随机的方式进行,每次生成的数字

I need that numbers to be generated in random manner everytime.

推荐答案

虽然不是8位数,我会使用一个 GUID 用于这一目的:

Although not 8 digits, I'd use a GUID for that purpose:

var id = Guid.NewGuid().ToString()

维基百科:

理想情况下,一个GUID绝不会
  通过任何一台计算机产生两次或
  组存在的计算机。该
  唯一键的总数(2 ^ 128或
  3.4×10 ^ 38 - 相对于有大约1.33×10 ^ 50在地球上的原子)是如此之大,相同的概率
  正在生成数量的两倍为
  非常小,并且某些
  技术已被开发,以帮助
  保证号码不重复。

Ideally, a GUID will never be generated twice by any computer or group of computers in existence. The total number of unique keys (2^128 or 3.4×10^38 - in relation there are about 1.33×10^50 atoms on earth) is so large that the probability of the same number being generated twice is extremely small, and certain techniques have been developed to help ensure that numbers are not duplicated

这篇关于生成在c#8位uinque ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆