在c#或vb.net中每年创建一个递增的序列号

时间:2015-10-26 09:27:23

标签: c# vb.net numbers sequence

有没有办法每年创建一个序列号作为学生ID 它应该包含年份+序列号 像这样:20150001/20160001

是否可能在代码背后......

任何形式的帮助都将不胜感激。

3 个答案:

答案 0 :(得分:1)

最后我发现了一个简单的代码:

Function GetID() As Long
    Dim vyear As Integer = 2015
    Dim vsno As Long
    cmd1.Connection = con1
    VSQL = "select max(ID) as vID from Table"
    cmd1.CommandText = VSQL
    cmd1.Connection = conOra
    rdr1 = cmd1.ExecuteReader
    rdr1.Read()
    If rdr1.HasRows Then
        If TypeName(rdr1!vID) <> "DBNull" Then
            vsno = rdr1!vID + 1
            GetID = vyear & vsno
        Else
            GetID = vyear & "0001"
        End If
    End If
    rdr1.Close()
    con1.close()
End Function

答案 1 :(得分:0)

您可以尝试类似下面的内容..

Dim SequenceNo as String = 'whatever you want the start number to be
Dim Year as String = 'This would be set to the year
Dim RequiredOutput as String = 'this would be set to the number of students you have
Dim StudentID as New List(Of String)

Do Until SequenceNo > RequiredOutput 
   StudentID.Add(Year & SequenceNo)
   SequenceNo += 1
loop

答案 2 :(得分:0)

您可以创建生成下一个序列号的方法,该序列号可能需要一年(或录取日期),学生ID可能如下所示。

  private static string GenerateNextSequence(DateTime addmissionDate, int studentNumber, int maxPad = 4) {
        var studentNumberString = studentNumber.ToString();
        return
            string.Format("{0}{1}{2}", addmissionDate.Year,
                (new StringBuilder()).Append('0', maxPad - studentNumberString.Length), studentNumberString);

    }

上述方法中的maxPad是学生最大长度(如果最大学生为9999,则长度为4)