提问人:siwa86 提问时间:10/18/2023 最后编辑:siwa86 更新时间:10/18/2023 访问量:101
如何使用 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
问:
如何使用 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
答:
1赞
User12345
10/18/2023
#1
您可以在此处添加列(使用您的代码进行调整)。我们需要额外的列来包含智能代码并检查ItemTransferIn
MonthYear
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
我的最新帖子仍然与您的答案有关,也许您可以帮助链接
评论
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