唯一编号的生成 [英] generation of unique number

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

问题描述


我在C#中使用Windows应用程序.第一次运行表单时,我想在文本框中生成ID号.而第二次,生成的数字必须是旧数字的+1.可以设置为10000到20000之间的条件.例如:第一次00154表示下次运行格式为00155的下一次.


im using windows application in c#. i want to generate a ID no in a text box when i run the form first time. while second time, the number generated must be +1 of the old number. can be given condition like between 10000 and 20000.for eg: 00154 for first time means next time when i run the form i want 00155.

推荐答案

第一个问题是为什么?

如果您要将其用作表中的主键,请不要使用.
The first question would be WHY?

If you''re going to use this as a primary key in a table, DON''T!


此处是生成随机数和字符串的示例..

希望它可以帮助您理解逻辑.并且您可以创建所需的任何条件.


Here is an example to generate random number as well as string..

Hope it helps you to understand the logic..and you can create whatever condition you want.


StringBuilder builder = new StringBuilder();
        builder.Append(RandomString(4, true));
        builder.Append(RandomNumber(1000, 9999));



产生随机数



Generate random number

private string RandomString(int size, bool lowerCase)
    {
        StringBuilder builder = new StringBuilder();
        Random random = new Random();
        char ch;
        for (int i = 0; i < size; i++)
        {
            ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            builder.Append(ch);
        }
        if (lowerCase)
            return builder.ToString().ToLower();
        return builder.ToString();
    }



生成随机数



Generate Random Number

private int RandomNumber(int min, int max)
{
    Random random = new Random();
    return random.Next(min, max);
}



您也可以应用查询来生成序列号



You may also apply query to generate serial number

string sqlQuery = "select max(" + colName + ") + 1 from " + tblName;



如果您还有任何疑问,请随时询问.
编码愉快.



If you still have any query don''t hesitate to ask..
Happy coding.


这篇关于唯一编号的生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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