-6

无论如何我可以使用c#生成一个唯一的序列号。我们正在开发一个网站,我们在注册时为每个用户分配一个个人ID......我需要生成一个14位数字的字符串。

For eg.

AA 01 201 210 22

AA 02 201 210 22

.

.

AA 99 201 210 22

then it should start from

AB 01 201 210 22

AB 02 201 210 22 and so on...

Last 8 digits are current date...

Thanks....



private string ConvertDecString(int value)
       {
            string CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            string retVal = string.Empty;
            int remainder = 0;
            do
            {
                remainder = value % 26;
                retVal = CHARS.Substring(remainder, 1) + retVal;
                value = (value / 26) - 1;
            }
            while ((value + 1) > 0);
            return retVal;
        }


public string GetCurrentDate()
    {
        string todaydate = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString();
        return todaydate;
    }

    private Random _random = new Random();

    public int RadomNum()
    {
        return _random.Next(10, 99);
    }

private string GetActualDate()
    {
        string Year = System.DateTime.Now.Year.ToString();
        string ActualDate1 = Year + GetProperMonth() + GetProperDay();
        int n = Convert.ToInt32(ActualDate1);
        string ActualDate = String.Format("{0:000 000 00}", n);
        return ActualDate;
    }

    private string GetProperMonth()
    {
        string Month = System.DateTime.Now.Month.ToString();
        string MonthNo = "";

        if (Month == "1")
        {
            MonthNo = "01";
        }
        else if (Month == "2")
        {
            MonthNo = "02";
        }
        else if (Month == "3")
        {
            MonthNo = "03";
        }
        else if (Month == "4")
        {
            MonthNo = "04";
        }
        else if (Month == "5")
        {
            MonthNo = "05";
        }
        else if (Month == "6")
        {
            MonthNo = "06";
        }
        else if (Month == "7")
        {
            MonthNo = "07";
        }
        else if (Month == "8")
        {
            MonthNo = "08";
        }
        else if (Month == "9")
        {
            MonthNo = "09";
        }
        else
        {
            MonthNo = Month;
        }
        return MonthNo;
    }


    private string GetProperDay()
    {
        string Day = System.DateTime.Now.Day.ToString();
        string DayNo = "";

        if (Day == "1")
        {
            DayNo = "01";
        }
        else if (Day == "2")
        {
            DayNo = "02";
        }
        else if (Day == "3")
        {
            DayNo = "03";
        }
        else if (Day == "4")
        {
            DayNo = "04";
        }
        else if (Day == "5")
        {
            DayNo = "05";
        }
        else if (Day == "6")
        {
            DayNo = "06";
        }
        else if (Day == "7")
        {
            DayNo = "07";
        }
        else if (Day == "8")
        {
            DayNo = "08";
        }
        else if (Day == "9")
        {
            DayNo = "09";
        }
        else
        {
            DayNo = Day;
        }
        return DayNo;
    }

    public string GeneratePatientNumber(string Alpha)
        {
            return ConvertDecString(Convert.ToInt16(Alpha)) + " " + RadomNum() + " " + GetActualDate();
        }

GeneratePatientNumber(string Alpha) - this method generates 12 digit personal id...
4

3 回答 3

0
公共类 IDGenerator
    {
        私人 int _firstLetter = 0;
        私人 int _secondLetter = 0;
        私人 int _firstDigitCounter = 90;
        私有字符串 _chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        公共字符串 NewID()
        {
            返回两个字母();
        }

        私有字符串 twoLetters()
        {
            _firstDigitCounter += 1;
            如果(_firstDigitCounter == 99)
            {
                _firstDigitCounter = 0;
                _secondLetter += 1;
            }

            if (_secondLetter > _chars.Length)
            {
                _secondLetter = 0;
                _firstLetter += 1;
            }

            返回 _chars.Substring(_firstLetter, 1) + _chars.Substring(_secondLetter, 1) + " " + _firstDigitCounter.ToString("00") + " " + DateTime.Now.Year.ToString().Insert(3, " " ) + DateTime.Now.Date.ToString("MM dd");
        }
    }

于 2012-10-22T10:00:20.773 回答
0
public string GetPersonalId (int n)
{
    char letter1 = (char)('A' + ((n / 10 / 26 / 26) % 26));
    char letter2 = (char)('A' + ((n / 10 / 26) % 26));
    char digit1 = (char)('0' + ((n / 10) % 10));
    char digit2 = (char)('0' + ((n) % 10));

    string dateString = string.Format("{0:yyyyMMdd}", DateTime.Today)
        .Insert(6, " ")
        .Insert(3, " ");

    return string.Format("{0}{1} {2}{3} {4}",
        letter1, letter2, digit1, digit2, dateString);
}

public GetNextSequenceForToday()
{
    SqlCommand query = new SqlCommand()
    query.CommandText =
       "SELECT COUNT(*) FROM [Users] " +
       "WHERE [Date] >= @today AND [Date] < @tomorrow";
    query.Parameters.Add("@today", DateTime.Today);
    query.Parameters.Add("@tomorrow", DateTime.Today.AddDays(1));

    int count = Convert.ToInt32(query.ExecuteScalar());

    return count + 1;
}
于 2012-10-22T09:13:08.287 回答
-1

您可以使用 Guid 类创建一个唯一的 32 个字符长度的字符串。

Guid g;
g = Guid.NewGuid();
Console.WriteLine(g);
Console.WriteLine(Guid.NewGuid());

C&P 来自:http: //msdn.microsoft.com/es-es/library/system.guid.newguid.aspx#Y330

于 2012-10-22T08:53:48.693 回答