我在VB.Net中有一个发货程序,在发货时会写入Access数据库。处理期间发生两个插入语句:第一个插入语句记录一般包信息。
Private Function AddToShipmentDatabase()
'Prevent null errors from database by changing empty fields to empty strings
If ShipmentInformation.CreditCardInfo = Nothing Then
ShipmentInformation.CreditCardInfo = ""
End If
If ShipmentInformation.Tracking = Nothing Then
ShipmentInformation.Tracking = ""
End If
If ShipmentInformation.TransactionId = Nothing Then
ShipmentInformation.TransactionId = ""
End If
'Connect to local database and upload the information
'stored in the shipment information structure
Dim strConnectionString As String = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=" & My.Settings.DBFileLocation
Dim objConnection As New OleDb.OleDbConnection(strConnectionString)
Dim objCommand As New OleDbCommand
objCommand.Connection = objConnection
objCommand.CommandText = "INSERT INTO [MailLog] ([CustomerId], [ShipDate], [CustomerName], [Service], [Tracking], [Address], [RxCount], [Charge], [CreditCardInfo], [TransactionId]) VALUES(@CustomerId, @ShipDate, @CustomerName, @Service, @Tracking, @Address, @RxCount, @Charge, @CreditCardInfo, @TransactionId)"
objCommand.Parameters.AddWithValue("@CustomerId", ShipmentInformation.CustomerId)
objCommand.Parameters.AddWithValue("@ShipDate", ShipmentInformation.ShipDate)
objCommand.Parameters.AddWithValue("@CustomerName", ShipmentInformation.CustomerName)
objCommand.Parameters.AddWithValue("@Service", ShipmentInformation.Service)
objCommand.Parameters.AddWithValue("@Tracking", ShipmentInformation.Tracking)
objCommand.Parameters.AddWithValue("@Address", ShipmentInformation.Address)
objCommand.Parameters.AddWithValue("@RxCount", ShipmentInformation.RxCount)
objCommand.Parameters.AddWithValue("@Charge", ShipmentInformation.Charge)
objCommand.Parameters.AddWithValue("@CreditCardInfo", ShipmentInformation.CreditCardInfo)
objCommand.Parameters.AddWithValue("@TransactionId", ShipmentInformation.TransactionId)
'Connect to database and add record
Dim intRecord As Integer = Nothing
objConnection.Open()
objCommand.ExecuteNonQuery()
Do Until intRecord <> Nothing
objCommand.CommandText = "SELECT @@IDENTITY"
intRecord = objCommand.ExecuteScalar()
objConnection.Close()
Loop
Return intRecord
End Function
第二个“插入”是包内容的数据网格的详细列表。第一个返回第二个列表中使用的记录ID,以便将货件详细信息与货件匹配
Private Sub LogPackage(ByVal RecordId As Integer)
'Loop through the records in the log and add to database
For i As Integer = 0 To (dgLog.Rows.Count - 1)
Dim strValues(4) As String
'Id
strValues(0) = RecordId
'RxNumber
strValues(1) = dgLog.Rows(i).Cells(3).Value.ToString
'DrugName
strValues(2) = dgLog.Rows(i).Cells(6).Value.ToString
'Quantity
strValues(3) = dgLog.Rows(i).Cells(4).Value.ToString
'Charge
strValues(4) = dgLog.Rows(i).Cells(5).Value.ToString
'Connect to local database and upload the information
'stored in the log datagrid
Dim strConnectionString As String = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=" & My.Settings.DBFileLocation
Dim objConnection As New OleDb.OleDbConnection(strConnectionString)
Dim objCommand As New OleDbCommand
objCommand.Connection = objConnection
objCommand.CommandText = "INSERT INTO [ShipmentDetails] ([MailLogId], [RxNumber], [DrugName], [Quantity], [Charge]) VALUES(@MailLogId, @RxNumber, @DrugName, @Quantity, @Charge)"
objCommand.Parameters.AddWithValue("@MailLogId", strValues(0))
objCommand.Parameters.AddWithValue("@RxNumber", strValues(1))
objCommand.Parameters.AddWithValue("@DrugName", strValues(2))
objCommand.Parameters.AddWithValue("@Quantity", strValues(3))
objCommand.Parameters.AddWithValue("@Charge", strValues(4))
'Connect to database and add record
objConnection.Open()
objCommand.ExecuteNonQuery()
objConnection.Close()
Next
End Sub
我的问题是,在某些情况下,包内容不会被写入。更具体地说,当插入失败时,一个项目丢失。如果发运了两件商品,则只记录一件。三个发货,两个记录。等等......我很感激任何帮助来解决这个问题。谢谢!
答案 0 :(得分:0)
我刚刚在insert语句中添加了一个循环,并将execute non-query的结果设置为整数。现在insert语句循环,直到insert = 1
的结果dim intRecord as integer = Nothing
Do Until intRecord = 1
//CODE
//CODE
//CODE
intRecord = objCommand.ExecuteNonQuery()
Loop
到目前为止似乎有效。谢谢!