0
我有一个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
第二个'插入'是软件包内容的数据网格的详细列表。第一个返回在第二上市用于装运细节匹配的出货量
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
我的问题记录ID是在occassion的软件包的内容不会被写入。更具体地说,一个项目在插入失败时丢失。如果两个项目出货,只有一个被记录。三个运送,两个记录。等等...我将不胜感激任何帮助解决这个问题。谢谢!