如何使用 Dapper 的 MS ACCESS 数据库中每个事务的序列号创建连续事务编号 VB.NET

How to create sequential transaction numbers with the serial number of each transaction in the MS ACCESS database in Dapper with VB.NET

提问人:siwa86 提问时间:10/18/2023 最后编辑:siwa86 更新时间:10/18/2023 访问量:101

问:

如何使用 VB.NET 在 Dapper 的 MS ACCESS 数据库中使用每个事务的序列号创建顺序事务编号? 如果月份发生变化,是否可以将序列号重置为?00001

也许我发布的代码仍然错误。请指导我

谢谢

Public Class Form1
    Private HeaderInvno As Integer
    Dim sis As New Stocksinservice()
    Private Sub CreateInvno()
        Dim myDate As DateTime = DateTime.Now
        Dim strTime As String = myDate.ToString("MMyy-")
        Dim Stockin = sis.SelectTop()
        If Stockin Is Nothing Then
            HeaderInvno = 1
        Else
            HeaderInvno = Stockin.HeaderInvno + 1
        End If
        If ComboBox1.Text = "ITEM TRANSFER IN" Then
            BtxtInvoNo.Text = "DEPT-ITI-" & strTime & (HeaderInvno).ToString("00000")
        ElseIf ComboBox1.Text = "PURCHASE INVOICE" Then
            BtxtInvoNo.Text = "DEPT-PI-" & strTime & (HeaderInvno).ToString("00000")
        ElseIf ComboBox1.Text = "RECEIVE ITEM" Then
            BtxtInvoNo.Text = "DEPT-RI-" & strTime & (HeaderInvno).ToString("00000")
        End If
    End Sub
    Private Sub ComboBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedValueChanged
        CreateInvno()
    End Sub
End Class
Public Class Stocksinservice
    Private connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\DEMO.accdb;Persist Security Info=False;"
    Public Function SelectTop() As Stocksin
        Dim sql = $"SELECT TOP 1 HeaderInvno FROM Stocksin ORDER BY HeaderInvno DESC"
        Using _conn = New OleDbConnection(connectionString)
            Return _conn.Query(Of Stocksin)(sql).FirstOrDefault()
        End Using
    End Function
End Class
Public Class Stocksin
    Public Property Invno() As String
    Public Property HeaderInvno() As Integer
End Class

桌子Stocksin

这是存储的数据库记录

因夫诺 标题Invno InvnoDate
部门-ITI-1023-00001 1 18-10-2023

因此,如果我从组合框中选择,结果如下:

DEPT-ITI-1023-00002
DEPT-PI-1023-00002
DEPT-RI-1023-00002

所需的结果应如下所示:

DEPT-ITI-1023-00002
DEPT-PI-1023-00001
DEPT-RI-1023-00001
SQL vb.net MS-Access Dapper

评论

1赞 Nick.Mc 10/18/2023
如果月份发生变化,是否可以将序列号重置为 00001,这对您来说真的至关重要吗?序列号的一点是它是唯一的。如果您每个月重置序列号,那么您还需要知道该月份和年份,以使其唯一
0赞 Nick.Mc 10/18/2023
实际上,在表格中使用自动编号(一个永远递增的数字)更容易,然后在记录中添加一些计算,使其看起来像您想要的样子。
0赞 siwa86 10/18/2023
@Nick.Mc ,为此我们可以从If you reset the serial number every month then you need to also know that month and year in order to make it unique InvnoDate
1赞 Nick.Mc 10/18/2023
将序列号格式附加到现实世界的事物上是一个主意。因为现实世界在变化,突然间你所有的序列号都没有意义了......比如你有没有想过一个月内有超过 99999 笔交易?如果你只是在幕后有一个“适当”的无限递增序列号,它将涵盖所有内容,你可以决定事后如何格式化它。另一个问题是,有人紧随其后,因为它看起来很整洁,还是因为它实际上是你的业务流程所必需的?
1赞 June7 10/18/2023
生成自定义唯一标识符是一个常见主题。您是否担心顺序上的差距?可以在需要时计算此序列号。一种方法是使用排序和分组以及文本框 RunningSum 属性对报表进行处理。但是,如果必须保存到表格中,请查看 accessforums.net/showthread.php?t=23329

答:

1赞 User12345 10/18/2023 #1

您可以在此处添加列(使用您的代码进行调整)。我们需要额外的列来包含智能代码并检查ItemTransferInMonthYear

ALTER TABLE YourTableName
ADD COLUMN ItemTransferIn VARCHAR(255),
ADD COLUMN MonthYear VARCHAR(6);

通过在选择顶部时使用条件参数来更新 select 方法,然后在月份更改时进行更新。ItemTransferIn

Public Class Form1
    Private HeaderInvno As Integer
    Private CurrentMonthYear As String
    Dim sis As New Stocksinservice()
    Private ItemTransferIn As String ' Define ItemTransferIn as a string

    Private Sub CreateInvno()
        Dim myDate As DateTime = DateTime.Now
        Dim strTime As String = myDate.ToString("yyMM-")
        Dim newMonthYear As String = myDate.ToString("yyMM")

        ' Set ItemTransferIn based on ComboBox1.Text
        If ComboBox1.Text = "ITEM TRANSFER IN" Then
            ItemTransferIn = "DEPT-ITI-"
        ElseIf ComboBox1.Text = "PURCHASE INVOICE" Then
            ItemTransferIn = "DEPT-PI-"
        ElseIf ComboBox1.Text = "RECEIVE ITEM" Then
            ItemTransferIn = "DEPT-RI-"
        End If

        ' Retrieve the HeaderInvno and MonthYear from the database
        Dim stockin = sis.SelectTop(ItemTransferIn )
        If stockin IsNot Nothing Then
            HeaderInvno = stockin.HeaderInvno
            CurrentMonthYear = stockin.MonthYear
        Else
            HeaderInvno = 0
            CurrentMonthYear = ""
        End If

        ' Check if the month and year have changed
        Dim newMonthYear As String = myDate.ToString("yyMM")
        If Not newMonthYear.Equals(CurrentMonthYear) Then
            HeaderInvno = 1
            CurrentMonthYear = newMonthYear
            ' Update the MonthYear in the database
            sis.UpdateMonthYear(CurrentMonthYear, ItemTransferIn )
        Else
            HeaderInvno += 1
        }

        ' Concatenate ItemTransferIn with other parts of the invoice number
        BtxtInvoNo.Text = ItemTransferIn & strTime & HeaderInvno.ToString("00000")
    End Sub

    Private Sub ComboBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedValueChanged
        CreateInvno()
    End Sub
End Class

还有另一个班级Stocksinservice

Public Class Stocksinservice
    Private connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\DEMO.accdb;Persist Security Info=False"
    
    Public Function SelectTop(itemTransferIn As String) As Stocksin
        Dim sql = "SELECT TOP 1 HeaderInvno, MonthYear FROM Stocksin WHERE ItemTransferIn = @ItemTransferIn ORDER BY HeaderInvno DESC"
        Using _conn = New OleDbConnection(connectionString)
            Return _conn.Query(Of Stocksin)(sql, New With {Key .ItemTransferIn = itemTransferIn}).FirstOrDefault()
        End Using
    End Function

    Public Sub UpdateMonthYear(monthYear As String, itemTransferIn As String)
        Dim sql = "UPDATE YourTableName SET MonthYear = @MonthYear WHERE ItemTransferIn = @ItemTransferIn" ' Replace YourTableName with the actual table name
        Using _conn = New OleDbConnection(connectionString)
            _conn.Execute(sql, New With {Key .MonthYear = monthYear, .ItemTransferIn = itemTransferIn})
        End Using
    End Sub
End Class

评论

0赞 siwa86 10/19/2023
谢谢你的完美回答,对不起,我回复晚了。也许您需要稍微修复行代码中的几行,对于此代码,我认为没有必要,因为最终事务将入到数据库表中。但我认为你的答案是完美的。missing Check if the month and year have changed for code Dim newMonthYear As String = myDate.ToString("yyMM") double variable, missing end if and remove }sis.UpdateMonthYear(CurrentMonthYear, ItemTransferIn)
0赞 siwa86 10/19/2023
我的最新帖子仍然与您的答案有关,也许您可以帮助链接