2014-01-28 59 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