如何为员工ID文本框创建唯一的自动密钥 [英] How do I create a unique automatic key for my employee id textbox
本文介绍了如何为员工ID文本框创建唯一的自动密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试为我的id文本框创建一个唯一的ID号我尝试了我的代码但是没有用。一旦点击新的记录按钮,这应该工作
我尝试过:
i am trying to create a unique id number for my id textbox i tried my code but doesnt work. This should work once the new record button is clicked
What I have tried:
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-" & GetUniqueKey(6)
End Sub
Private Sub NewRecord_Click(sender As Object, e As EventArgs) Handles NewRecord.Click
AutoGenerate()
Reset()
End Sub
推荐答案
如果您将员工存储到数据库中,那么数据库的责任应由数据库负责给出一个唯一的身份证号码并确保始终保持唯一性。
如果您不使用数据库,那么您必须考虑其他方法。一种方法是使用Guid,参见 Guid.NewGuid方法(系统) [ ^ ]
If you're storing the employees to a database then it should be the database's responsibility to give an unique id number and ensure the uniqueness at all times.
If you're not using a database then you have to think some other approach. One way could be to utilize Guid, see Guid.NewGuid Method (System)[^]
这篇关于如何为员工ID文本框创建唯一的自动密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文