2011-06-09 56 views
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的軟件包的內容不會被寫入。更具體地說,一個項目在插入失敗時丟失。如果兩個項目出貨,只有一個被記錄。三個運送,兩個記錄。等等...我將不勝感激任何幫助解決這個問題。謝謝!

回答

0

我剛剛結束了向insert語句添加循環並將執行非查詢的結果設置爲整數。現在插入語句循環直到插入的結果= 1

dim intRecord as integer = Nothing 
Do Until intRecord = 1 
//CODE 
//CODE 
//CODE 
intRecord = objCommand.ExecuteNonQuery() 
Loop 

到目前爲止它似乎工作。謝謝!