【问题标题】:Auto generate alpha numeric in VB.NET在 VB.NET 中自动生成字母数字
【发布时间】:2020-03-24 08:42:08
【问题描述】:

我目前正在处理我使用 VB.NET 2019 和 SQL Server 的项目。我需要创建一个自动生成 ID 的函数。

我希望我的 ID 如下所示:P001P002P003 等。有人可以告诉我如何编码吗?下面是我的代码

 Private Sub Form4_Load_1(sender As Object, e As EventArgs) Handles MyBase.Load
    BindData()
    Dim data As String = "Data Source=LAPTOP-M8KKSG0I;Initial Catalog=Oceania;Integrated Security=True"
    Dim con As New SqlConnection(data)
    Try
        If Con.State = ConnectionState.Closed Then
            con.Open()

        End If

        Dim sql As String = "Select Max(PatientID) from Patient"
        Dim cmd As New SqlCommand(sql, con)
        Dim Max As String = cmd.ExecuteScalar
        If Max > 0 Then
            TextBox1.Text = Max + 1
        Else
            TextBox1.Text = "P01"
        End If
    Catch ex As Exception
        MsgBox(Err.Description)
    End Try
End Sub

【问题讨论】:

  • SO 不是代码编写服务。由您来研究、思考并尽最大努力实施。如果它不起作用,那么您可以在此处发布问题,向我们展示您所做的并解释它如何不符合您的期望。在这种情况下,线索是唯一改变的部分是数字,所以你根本没有生成 alpha 部分。
  • 我建议您先查看IDENTITYSEQUENCE,然后再计算列。如果您遇到困难,请向我们展示您卡在哪里以及您编写的代码。否则,请向我们展示您所做的研究并解释您对此不了解的地方。另外,你应该考虑到'P999'会发生什么。
  • 我请你检查我的答案。
  • (从字符串 P04 到类型 'integer' 的转换无效)系统弹出此消息。我该怎么办
  • @jiayukoh 为 P001、P002 等创建一个 int identity 的 PatientID 列和另一个列 PatientCode Varchar(10) 。否则,您需要从 P004 中提取 4。最好的选择是按照我的回答中的建议使用计算列。

标签: sql-server vb.net


【解决方案1】:

你可以这样试试。这里1 是一个自动生成的数字,它可能是 SQL Server 中表的标识键列值。

Dim number As Integer = 1
Dim numberText As String = "P" & number.ToString().PadLeft(3, "0") 

直播demo

您可以在表中添加这样的计算列来自动生成序列。一旦多人同时输入,这将减少重复值运行时的机会。

Alter table Patient ADD PatientCode AS ('P' + Convert(Varchar(3),CONCAT(REPLICATE('0', 3 - LEN(PatientID)), PatientID)) )

要动态获取列值,您可以尝试下面的代码来生成函数。

Private Sub GenerateSequnce()
    Dim constring As String = "Data Source=TestServer;Initial Catalog=TestDB;User id = TestUser;password=test@123"
    Using con As New SqlConnection(constring)
        Using cmd As New SqlCommand("Select Top 1 ISNULL(TaxCode, 0) from Tax_Mst Order By TaxCode Desc", con)
            cmd.CommandType = CommandType.Text
            Using sda As New SqlDataAdapter(cmd)
                Using dt As New DataTable()
                    sda.Fill(dt)
                    Dim maxNumberCode = dt.Rows(0)("TaxCode").ToString()
                    If (maxNumberCode = "0") Then
                        maxNumberCode = "1"
                    End If

                    Dim numberText As String = "P" & maxNumberCode.ToString().PadLeft(3, "0")
                End Using
            End Using
        End Using
    End Using
End Sub

这里TaxCode 的列是intidentity 约束。

通过代码中的小修正,您可以实现如下所示。

Dim data As String = "Data Source=LAPTOP-M8KKSG0I;Initial Catalog=Oceania;Integrated Security=True"
        Dim con As New SqlConnection(data)
        Try
            If con.State = ConnectionState.Closed Then
                con.Open()
            End If

            Dim sql As String = "Select ISNULL(Max(PatientID), 0) from Patient"
            Dim cmd As New SqlCommand(sql, con)
            Dim Max As String = cmd.ExecuteScalar

            If (Max = "0") Then
                Max = "1"
            Else
                Max = CInt(Max) + 1
            End If
            Dim numberText As String = "P" & Max.ToString().PadLeft(3, "0")
            TextBox1.Text = numberText

        Catch ex As Exception
            MsgBox(Err.Description)
       End Try

输出

【讨论】:

  • "这里的 1 是一个自动生成的数字,它可能是 SQL Server 中表的标识键列值。" - 什么?它是您代码中的一个变量。您的代码如何生成P002P003 等?
  • “执行 SQL 命令后,数字将来自数据表”您如何避免出现竞争条件?
  • 那么这个数字是从哪里来的,@SurajKumar?如果 OP 自动生成这个值(大概在INSERT),你怎么知道“下一个”数字是什么?
  • @SurajKumar - 你从哪里得到数字来自数据库的要求?但是,如果您假设确实如此,那么您应该在答案中表明这一点。像这样定义number 具有误导性。
  • 所以我回到我原来的问题:“你如何避免这种情况的竞争条件?”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-17
  • 1970-01-01
  • 1970-01-01
  • 2021-07-10
  • 1970-01-01
  • 2017-01-17
  • 1970-01-01
相关资源
最近更新 更多