【发布时间】:2011-06-09 03:21:41
【问题描述】:
我在 VB.Net 中有一个发货程序,它在处理发货时写入 Access DB。在处理过程中会出现两个插入语句: 第一个插入语句记录一般包信息。
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
我的问题是有时没有写入包内容。更具体地说,当插入失败时,缺少一项。如果运送了两件物品,则只记录一件。三个已发货,两个已记录。等等......我将不胜感激任何帮助来解决这个问题。谢谢!
【问题讨论】:
标签: .net database ms-access insert