如何使用VB.NET在Dapper中的MS ACCESS数据库中使用每笔交易的序列号创建连续的交易编号

问题描述 投票:0回答:1

如何使用 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 2023年10月18日

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

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个回答
0
投票

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

ItemTransferIn
上的智能代码并检查
MonthYear 

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

选择顶部时通过为

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
© www.soinside.com 2019 - 2024. All rights reserved.