Sql将数据插入两次访问

时间:2014-01-28 11:00:55

标签: vb.net visual-studio-2010 visual-studio ms-access

我有一个简单的查询,可以将数据插入到访问2010 db中。但是,运行查询时,它会将数据插入两次而不是一次。我按下按钮点击查询,只调用一次。我验证了设置断点并观察代码运行情况,从我所看到的情况来看,只有1个条目但是访问权限2.有人可以帮助我理解为什么会发生这种情况或帮助我进一步调试。非常感谢

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()

1 个答案:

答案 0 :(得分:1)

原因是您在代码中写了两次oledbCmd.ExecuteNonQuery()。请参阅代码中的以下行。

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