如何使用Access数据库在VB.NET中生成唯一ID?

时间:2017-01-11 10:15:46

标签: vb.net

我想为员工ID文本框生成一个唯一ID,并将其保存到Access中的数据库中。

我试过这个,但是当我构建代码时,我看到没有错误,但它仍然不起作用。

Public Shared Function GetUniqueKey(ByVal maxSize As Integer) As String
    Dim chars As Char() = New Char(61) {}
    chars = "123456789".ToCharArray()
    Dim data As Byte() = New Byte(0) {}
    Dim crypto As New RNGCryptoServiceProvider()
    crypto.GetNonZeroBytes(data)
    data = New Byte(maxSize - 1) {}
    crypto.GetNonZeroBytes(data)
    Dim result As New StringBuilder(maxSize)
    For Each b As Byte In data
        result.Append(chars(b Mod (chars.Length)))
    Next
    Return result.ToString()
End Function

Public Sub AutoGenerate()
    EmployeeID.Text = "E-00" & GetUniqueKey(6)
End Sub

Private Sub New1_Click(sender As Object, e As EventArgs) Handles new1.Click
    AutoGenerate()
    Reset()
End Sub

0 个答案:

没有答案