如何在 vb.net 中使用年份和整数生成自定义 ID
how to generate custom id using year and integer in vb.net
我想使用年份和整数制作自定义 ID 号。
这是我的代码:
Dim idnumber As Integer
Dim yrVal As String = DateTime.Now.Year
idnumber = yrVal + "0001"
textboxIDNumber.Text = idnumber
Connection()
sql = "SELECT * FROM Info WHERE idnumber LIKE '" & textboxIDNumber.Text & "'"
rs.Open(sql, conn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
If rs.Fields(0).Value <> idnumber Then
rs.AddNew()
rs.Fields(0).Value = textboxIDNumber.Text
rs.Fields(1).Value = textboxLastName.Text
rs.Fields(2).Value = textboxFirstName.Text
rs.Fields(3).Value = textboxMiddleName.Text
rs.Update()
MsgBox("Successfully Added!", MsgBoxStyle.Information, "Message")
Else
If rs.Fields(0).Value = idnumber Then
rs.AddNew()
textboxIDNumber.Text += 1
rs.Fields(0).Value = textboxIDNumber.Text
rs.Fields(1).Value = textboxLastName.Text
rs.Fields(2).Value = textboxFirstName.Text
rs.Fields(3).Value = textboxMiddleName.Text
rs.Update()
MsgBox("Successfully Added!", MsgBoxStyle.Information, "Message")
End If
End If
conn.Close()
我想添加一个自动生成的某个人的 ID 号,准确地说是使用 YEAR 和 4 位整数。
Dim idnumber As Integer
Dim yrVal As String = DateTime.Now.Year
idnumber = yrVal + "0001"
我要的是,如果我第一次注册一个人那么his/her身份证号码是20180001,第二个人是20180002,0003,004等等,但是当年份是2019的时候它再次从 20190001、20190002、20190003 等开始。在我的代码中,如上所示,它将从 20180001 增加到 20189999。我是初学者,很难理解。
How to generate Custom ID
我尝试了上面的解决方案 link,但它使用的是 OLeDBAdapter、数据集、数据网格,但我不知道如何将其转换为 ADODB。我添加了 ADODB 参考。我是初学者,有点困惑。
很简单...第一次用下面的代码生成一个id :
Dim idnumber As Integer
Dim yrVal As String = DateTime.Now.Year
idnumber = yrVal + "0001"
''''Insert the data, I am not adding the inserting code :)
现在,为了生成第二个 ID,我们需要先获取我们插入数据库的最后一个 ID。为此,请使用以下代码:
Dim LastId as Integer
Dim FirstIdCmd as New SqlCommand("SELECT TOP 1 * FROM TableName ORDER BY ID DESC",connection) 'Here ID is the column name and DESC stands for descending
Dim dr as SqlDataReader = FirstIdCmd.ExecuteReader
While dr.Read
'''Here we get the last generated I
Dim lstId as String = dr(0).ToString.Substring(4, 4)
End While
现在关注 dr(0).ToString.Substring(4,4) 行。 Strng.Substring
方法用于提取字符串的特定部分。该方法接受 2 个整数变量值。第一个是 StartingIndex
,最后一个是 Length
。由于我们只需要获取 ID 的最后 3 位数字并且需要绕过前 4 位数字,我们将 StartIndex
设置为 4 并将 Length
设置为 4。所以,让我们再次回到编码:
While dr.Read
''Here,instead of creating a new string variable, we pass the value(with +1) to the declared Integer variable called LastID
LastId = Convert.ToInt32(dr(0).ToString.Substring(4, 4)) + 1
End While
现在您可以使用ID并插入它了。希望对你有帮助。
我想使用年份和整数制作自定义 ID 号。 这是我的代码:
Dim idnumber As Integer
Dim yrVal As String = DateTime.Now.Year
idnumber = yrVal + "0001"
textboxIDNumber.Text = idnumber
Connection()
sql = "SELECT * FROM Info WHERE idnumber LIKE '" & textboxIDNumber.Text & "'"
rs.Open(sql, conn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
If rs.Fields(0).Value <> idnumber Then
rs.AddNew()
rs.Fields(0).Value = textboxIDNumber.Text
rs.Fields(1).Value = textboxLastName.Text
rs.Fields(2).Value = textboxFirstName.Text
rs.Fields(3).Value = textboxMiddleName.Text
rs.Update()
MsgBox("Successfully Added!", MsgBoxStyle.Information, "Message")
Else
If rs.Fields(0).Value = idnumber Then
rs.AddNew()
textboxIDNumber.Text += 1
rs.Fields(0).Value = textboxIDNumber.Text
rs.Fields(1).Value = textboxLastName.Text
rs.Fields(2).Value = textboxFirstName.Text
rs.Fields(3).Value = textboxMiddleName.Text
rs.Update()
MsgBox("Successfully Added!", MsgBoxStyle.Information, "Message")
End If
End If
conn.Close()
我想添加一个自动生成的某个人的 ID 号,准确地说是使用 YEAR 和 4 位整数。
Dim idnumber As Integer
Dim yrVal As String = DateTime.Now.Year
idnumber = yrVal + "0001"
我要的是,如果我第一次注册一个人那么his/her身份证号码是20180001,第二个人是20180002,0003,004等等,但是当年份是2019的时候它再次从 20190001、20190002、20190003 等开始。在我的代码中,如上所示,它将从 20180001 增加到 20189999。我是初学者,很难理解。
How to generate Custom ID
我尝试了上面的解决方案 link,但它使用的是 OLeDBAdapter、数据集、数据网格,但我不知道如何将其转换为 ADODB。我添加了 ADODB 参考。我是初学者,有点困惑。
很简单...第一次用下面的代码生成一个id :
Dim idnumber As Integer
Dim yrVal As String = DateTime.Now.Year
idnumber = yrVal + "0001"
''''Insert the data, I am not adding the inserting code :)
现在,为了生成第二个 ID,我们需要先获取我们插入数据库的最后一个 ID。为此,请使用以下代码:
Dim LastId as Integer
Dim FirstIdCmd as New SqlCommand("SELECT TOP 1 * FROM TableName ORDER BY ID DESC",connection) 'Here ID is the column name and DESC stands for descending
Dim dr as SqlDataReader = FirstIdCmd.ExecuteReader
While dr.Read
'''Here we get the last generated I
Dim lstId as String = dr(0).ToString.Substring(4, 4)
End While
现在关注 dr(0).ToString.Substring(4,4) 行。 Strng.Substring
方法用于提取字符串的特定部分。该方法接受 2 个整数变量值。第一个是 StartingIndex
,最后一个是 Length
。由于我们只需要获取 ID 的最后 3 位数字并且需要绕过前 4 位数字,我们将 StartIndex
设置为 4 并将 Length
设置为 4。所以,让我们再次回到编码:
While dr.Read
''Here,instead of creating a new string variable, we pass the value(with +1) to the declared Integer variable called LastID
LastId = Convert.ToInt32(dr(0).ToString.Substring(4, 4)) + 1
End While
现在您可以使用ID并插入它了。希望对你有帮助。