-6

とにかく、c#を使用して一意のシーケンス番号を生成できますか?登録時に各ユーザーに個人IDを割り当てるWebサイトを開発しています...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
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 に答える
0
パブリッククラスIDGenerator
    {{
        private int _firstLetter = 0;
        private int _secondLetter = 0;
        private int _firstDigitCounter = 90;
        プライベート文字列_chars="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        パブリック文字列NewID()
        {{
            twoLetters();を返します。
        }

        プライベート文字列twoLetters()
        {{
            _firstDigitCounter + = 1;
            if(_firstDigitCounter == 99)
            {{
                _firstDigitCounter = 0;
                _secondLetter + = 1;
            }

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

            return _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 に答える
-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 に答える