2014-01-28 106 views
0

我有一個簡單的查詢,將數據插入訪問2010 db。但是,查詢運行時,會插入數據兩次而不是一次。我點擊按鈕觸發查詢,只被調用一次。我驗證了設置斷點並觀察代碼運行的情況,並且從我所看到的情況來看,只有1個條目,但訪問權限爲2個。有人可以幫助我理解爲什麼發生這種情況或者幫助我進一步調試。非常感謝Sql將數據插入到訪問

If CDbl(msg) > 0 And rdbBoxReturn.Checked = True Then 

      Dim custref As String 
      Dim box As String 
      Dim itmAs String 
      Dim itm2 As String 
      Dim Quantity As Integer = Convert.ToInt32(txtBoxQuantity.Text) 


      sql = "SELECT Max(Requests.[Request no]) AS [MaxOfRequest no] FROM Requests" 

      oledbCmd.CommandText = sql 
      oledbCmd.Connection = oledbCnn 

      dr = oledbCmd.ExecuteReader() 

      If dr.HasRows Then 

       While dr.Read 

        itm = CStr(dr.Item("MaxOfRequest no")) 
        itm = String.Format("{0:D6}", (Convert.ToInt32(itm) + 1)) 

       End While 

      End If 

      dr.Close() 

      Dim tran As OleDbTransaction = oledbCnn.BeginTransaction() 

      Try 
       ' Here the connection should be already open 

       oledbCmd.Transaction = tran 

       For i = 0 To lvSelectedItems.Items.Count - 1 
        box = lvSelectedItems.Items.Item(i).Text 
        custref = lvSelectedItems.Items.Item(i).SubItems.Item(1).Text 

        sql = "Insert into Requests ([Request no], Customer, Dept, [Type], [Service level], [Date-time received], [Received by], [Date-time due], Quantity, [Cust requestor], [Status]) Values (?, ?, ?, 'B', ?, ?, ?, ?, ?, ?, 'O')" 

        Debug.Print(sql) 

        oledbCmd.CommandText = sql 
        oledbCmd.Parameters.Clear() 
        oledbCmd.Connection = oledbCnn 
        oledbCmd.Parameters.AddWithValue("@p1", itm) 
        oledbCmd.Parameters.AddWithValue("@p2", cmbCustomer.Text) 
        oledbCmd.Parameters.AddWithValue("@p3", cmbDept.Text) 
        oledbCmd.Parameters.AddWithValue("@p4", rbServiceLevel.ToString) 
        oledbCmd.Parameters.AddWithValue("@p5", dtpDateReceived.Value.ToString) 
        oledbCmd.Parameters.AddWithValue("@p6", txtBy.Text) 
        oledbCmd.Parameters.AddWithValue("@p7", dtpDateDue.Value.ToString) 
        oledbCmd.Parameters.AddWithValue("@p8", Quantity) 
        oledbCmd.Parameters.AddWithValue("@p9", cmbRequestBy.Text) 
        oledbCmd.ExecuteNonQuery() 
        Dim rowsAffected = oledbCmd.ExecuteNonQuery() 
        If rowsAffected = 0 Then 
         ' Fail to insert. Display a message and rollback everything 
         tran.Rollback() 
         Return 
        End If 


        'End If 

       Next 
       ' if we reach this point, then all the commands have been 
       ' completed correctly we could commit everything 
       tran.Commit() 

      Catch ex As Exception 
       MessageBox.Show(ex.Message) 
       tran.Rollback() 

      End Try 

      MessageBox.Show("You have successfully completed the box return", "Box return successfull") 
      Close() 

     Else 

      MessageBox.Show("You must select a box") 

      'CType(sender, RadioButton).Checked = False 
      'Return 

     End If 

      dr.Close() 
      oledbCnn.Close() 
+0

用於獲取下一個請求編號的代碼在多用戶環境中是不安全的。 – Fionnuala

回答

1

的原因是因爲你已經在你的代碼編寫oledbCmd.ExecuteNonQuery()兩次。請參閱代碼中的以下行。

oledbCmd.ExecuteNonQuery()//Remove this line from your code and try 
Dim rowsAffected = oledbCmd.ExecuteNonQuery() 
+0

所以,我要刪除'oledbCmd.ExecuteNonQuery()'這一行並留下暗淡的語句。謝謝 – user1532468

+0

@ user1532468讓我知道它是否有效。 – Bhushan

+0

非常好。我的眼睛一定會變老:-)非常感謝 – user1532468