我的项目在创建新票证时生成连续的票号。 然而,在极少数情况下,这个数字重复。我已经分析了表格,发现当票证生成1到2分钟时会发生这种情况。
以下代码用于读取和填充文本框的票号;我使用Sql阅读器:
Sub loadTicketNumber()
Dim connetionString As String
Dim sqlCnn As SqlClient.SqlConnection
Dim sqlCmd As SqlClient.SqlCommand
Dim sql As String
connetionString = My.Settings.DBServer
sql = "Select TicketNo FROM tbl_TicketNumber WHERE PC= '" & Soft_Parameters.QS_ClientPC & "'"
sqlCnn = New SqlClient.SqlConnection(connetionString)
Try
sqlCnn.Open()
sqlCmd = New SqlClient.SqlCommand(sql, sqlCnn)
Dim sqlReader As SqlClient.SqlDataReader = sqlCmd.ExecuteReader()
Dim x As Integer
While sqlReader.Read()
x = sqlReader.Item("TicketNo")
End While
sqlReader.Close()
sqlCmd.Dispose()
sqlCnn.Close()
txtTicket.Text = x + 1
txt_ReTicketNo.Text = x
Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End Sub
但是当我保存我的SQL代码时会搜索Edit或New ticket;如果是新票,它会将新票号保存在另一个表中。
我会解释更多故障单编号将生成如果它的新票证,如果这个编辑的票证不会生成,这就是为什么编辑和添加新的是相同的子。
这是我的代码:
' these strings will dynamically generate at runtime i can add fields to form SQL statements
' EditString is variable for edit SQL strin
' AddString is a variable for Insert into statement that holds data table columns
' AddStringVal uses that holds above Column value
Try
con.Open()
Dim cm As New SqlClient.SqlCommand
cm.Connection = con
cm.CommandText = " DECLARE @tbl_ID TABLE (nID int) UPDATE tbl_Tickets " & _
" SET Ticket_Type = @Ticket_Type, VehicleNo= @VehicleNo," & _
" Customer=@Customer, Job_Code=@Job_Code, Material=@Material, " & _
" First_Weight=@First_Weight, Second_Weight=@Second_Weight, Net_Weight=@Net_Weight, " & _
" Add_Charges=@Add_Charges, Deduction=@Deduction, Ticket_Amount=@Ticket_Amount, " & _
" Source=@Source, Destination=@Destination, Operator=@Operator, Out_Time=@Out_Time, PC=@PC, Unit_Price=@Unit_Price" & _
EditString & _
" OUTPUT INSERTED.QS_Code INTO @tbl_ID (nID) WHERE (VehicleNo=@VehicleNo) AND (Second_Weight IS NULL) AND (Ticket_Type <>'DELETED') " & _
" IF @@ROWCOUNT=0 " & _
" BEGIN " & _
" INSERT INTO tbl_Tickets (Ticket_Type, TicketNo, VehicleNo, nDate, Customer,Job_Code,Material, " & _
" First_Weight, Second_Weight, Net_Weight, Unit_Price, Add_Charges, Deduction, Ticket_Amount,Source, " & _
" Destination, Operator, In_Time, PC " & _
AddString & " ) VALUES (@Ticket_Type, @TicketNo, @VehicleNo, @nDate, @Customer,@Job_Code,@Material, " & _
" @First_Weight, @Second_Weight, @Net_Weight, @Unit_Price, @Add_Charges, @Deduction, @Ticket_Amount,@Source, " & _
" @Destination, @Operator, @In_Time, @PC " & _
AddStringVal & " ) SELECT IDENT_CURRENT('tbl_Tickets') FROM tbl_Tickets " & _
" UPDATE tbl_TicketNumber " & _
" SET TicketNo = @TTicket, PC= @TPC, nDate= @TnDate" & _
" WHERE (PC=@TPC) " & _
" IF @@ROWCOUNT=0 " & _
" BEGIN " & _
" INSERT INTO tbl_TicketNumber (TicketNo, PC, nDate) VALUES (@TTicket,@TPC,@TnDate) END " & _
" END SELECT * From @tbl_ID"
'this will pass parameters to update tbl_TicketNumber that stores my last ticket number
cm.Parameters.AddWithValue("@TTicket", Val(txtTicket.Text))
cm.Parameters.AddWithValue("@TPC", Soft_Parameters.QS_ClientPC)
cm.Parameters.AddWithValue("@TnDate", Now)
'this will pass parameters to update tbl_Tickets that stores all my ticket details
cm.Parameters.AddWithValue("@Ticket_Type", cboTicketType.Text)
cm.Parameters.AddWithValue("@TicketNo", Val(txtTicket.Text))
cm.Parameters.AddWithValue("@VehicleNo", cboV_Code.Text)
cm.Parameters.AddWithValue("@nDate", dtpDate.Value)
cm.Parameters.AddWithValue("@Customer", Val(cboCustomer.SelectedValue))
cm.Parameters.AddWithValue("@Job_Code", Val(cboJobCode.SelectedValue))
cm.Parameters.AddWithValue("@Material", Val(cboMaterial.SelectedValue))
cm.Parameters.AddWithValue("@First_Weight", Val(txtFWeight.Text))
If Val(txtSWeight.Text) > 0 Then cm.Parameters.AddWithValue("@Second_Weight", Val(txtSWeight.Text)) Else cm.Parameters.AddWithValue("@Second_Weight", DBNull.Value)
If Val(txtNetWeight.Text) > 0 Then cm.Parameters.AddWithValue("@Net_Weight", Val(txtNetWeight.Text)) Else cm.Parameters.AddWithValue("@Net_Weight", DBNull.Value)
If txtUnitPrice.Visible = True Then cm.Parameters.AddWithValue("@Unit_Price", Val(txtUnitPrice.Text)) Else cm.Parameters.AddWithValue("@Unit_Price", DBNull.Value)
If txtAddition.Visible = True Then cm.Parameters.AddWithValue("@Add_Charges", Val(txtAddition.Text)) Else cm.Parameters.AddWithValue("@Add_Charges", DBNull.Value)
If txtDeduction.Visible = True Then cm.Parameters.AddWithValue("@Deduction", Val(txtDeduction.Text)) Else cm.Parameters.AddWithValue("@Deduction", DBNull.Value)
If txtTktPrice.Visible = True Then cm.Parameters.AddWithValue("@Ticket_Amount", Val(txtTktPrice.Text)) Else cm.Parameters.AddWithValue("@Ticket_Amount", DBNull.Value)
If cboSource.Visible = True Then cm.Parameters.AddWithValue("@Source", cboSource.Text) Else cm.Parameters.AddWithValue("@Source", DBNull.Value)
If cboDestination.Visible = True Then cm.Parameters.AddWithValue("@Destination", cboDestination.Text) Else cm.Parameters.AddWithValue("@Destination", DBNull.Value)
cm.Parameters.AddWithValue("@Operator", txtOperator.Text)
cm.Parameters.AddWithValue("@Out_Time", Now)
cm.Parameters.AddWithValue("@In_Time", Now)
cm.Parameters.AddWithValue("@PC", Soft_Parameters.QS_ClientPC)
lblQS_Code.Text = Convert.ToInt32(cm.ExecuteScalar.ToString())
Catch ex As Exception
txtError.Text = ex.Message
Finally
If Not con.State = ConnectionState.Closed Then con.Close()
con.Close()
'if this save is not firstweight edit then it will save new ticket no and generate new ticket
If TicketFirstweightEdit = False Then
' SaveTicketNumber()
loadTicketNumber()
Else
TicketFirstweightEdit = False
End If
'after saving this will clear fields automatically
MsgBox("Data has been Updated Successfully!", MsgBoxStyle.OkOnly, "Data Updated!!")
End Try
忘记我传递的字符串部分
DECLARE @tbl_ID TABLE (nID int)
UPDATE tbl_Tickets
SET Ticket_Type = @Ticket_Type, VehicleNo= @VehicleNo,
Customer=@Customer, Job_Code=@Job_Code, Material=@Material,
First_Weight=@First_Weight, Second_Weight=@Second_Weight, Net_Weight=@Net_Weight,
Add_Charges=@Add_Charges, Deduction=@Deduction, Ticket_Amount=@Ticket_Amount,
Source=@Source, Destination=@Destination, Operator=@Operator, Out_Time=@Out_Time, PC=@PC, Unit_Price=@Unit_Price
OUTPUT INSERTED.QS_Code INTO @tbl_ID (nID) WHERE (VehicleNo=@VehicleNo) AND (Second_Weight IS NULL) AND (Ticket_Type <>'DELETED')
IF @@ROWCOUNT=0
BEGIN
INSERT INTO tbl_Tickets (Ticket_Type, TicketNo, VehicleNo, nDate, Customer,Job_Code,Material,
First_Weight, Second_Weight, Net_Weight, Unit_Price, Add_Charges, Deduction, Ticket_Amount,Source,
Destination, Operator, In_Time, PC)
VALUES (@Ticket_Type, @TicketNo, @VehicleNo, @nDate, @Customer,@Job_Code,@Material,
@First_Weight, @Second_Weight, @Net_Weight, @Unit_Price, @Add_Charges, @Deduction, @Ticket_Amount,@Source,
@Destination, @Operator, @In_Time, @PC)
SELECT IDENT_CURRENT('tbl_Tickets') FROM tbl_Tickets
UPDATE tbl_TicketNumber
SET TicketNo = @TTicket, PC= @TPC, nDate= @TnDate
WHERE (PC=@TPC)
IF @@ROWCOUNT=0
BEGIN
INSERT INTO tbl_TicketNumber (TicketNo, PC, nDate) VALUES (@TTicket,@TPC,@TnDate)
END
END
SELECT * From @tbl_ID
答案 0 :(得分:1)
将loadTicket sub更改为
Sub loadTicketNumber()
Dim connetionString As String
Dim sqlCnn As SqlClient.SqlConnection
Dim sqlCmd As SqlClient.SqlCommand
Dim sql As String
connetionString = My.Settings.DBServer
sql = String.Format("Select ISNULL(MAX(TicketNo), 0) TicketNo FROM tbl_TicketNumber WHERE PC= '{0}'", Soft_Parameters.QS_ClientPC)
Using sqlCnn SqlClient.SqlConnection = New SqlClient.SqlConnection(connetionString)
Try
sqlCnn.Open()
Using SqlClient.SqlCommand sqlCmd = New SqlClient.SqlCommand(sql, sqlCnn)
Dim x As Integer
x = Convert.ToInt32(sqlCmd.ExecuteScalar());
End Using
txtTicket.Text = x + 1
txt_ReTicketNo.Text = x
Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End Using
End Sub
这不仅运行得更快,而且还会正确处理对象,因为你的try中没有finally,如果有问题,你就不会丢弃connection
或command
。
但我无法看到为什么会创建重复的TicketNo。但代码很难阅读。我建议你更好地格式化它。
为什么在SELECT IDENT_CURRENT('tbl_Tickets') FROM tbl_Tickets
之后运行Insert
?因为它似乎没有任何用途?