Sql将数据两次插入访问

Posted

技术标签:

【中文标题】Sql将数据两次插入访问【英文标题】:Sql inserting data twice into access 【发布时间】:2014-01-28 11:00:55 【问题描述】:

我有一个简单的查询,可将数据插入到 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】:

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

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

【讨论】:

所以我要删除'oledbCmd.ExecuteNonQuery()'这一行并留下dim 语句。谢谢

以上是关于Sql将数据两次插入访问的主要内容,如果未能解决你的问题,请参考以下文章

ms 访问转储到 sql 插入

使用java和SQL查询INSERT INTO将新记录插入MS访问[关闭]

从访问数据库中选择表并在c#上插入sql多个表

如何将从 ms 访问表中检索到的记录插入到 sql server 表中?

Sql插入查询未插入

在 For 循环两次 Oracle 中访问值