【问题标题】:ExecuteNonQuery requires the command to have a transaction when connection assigned to the command is in a pending local transaction当分配给命令的连接处于挂起的本地事务中时,ExecuteNonQuery 要求该命令具有事务
【发布时间】:2017-07-10 12:32:12
【问题描述】:

我在通过命令文本执行查询时遇到问题。我想要做的是从数据网格和数据库中删除数据,并在用户选择时重新插入。

错误是:

当分配给命令的连接处于挂起的本地事务中时,ExecuteNonQuery 要求该命令具有事务。

这是我删除的代码:

Private Sub DeleteRecord(ByVal month As Integer)

    Dim cmd As New SqlClient.SqlCommand
    cmd.Connection = Connection
    cmd.Transaction = transaction
    cmd.CommandText = "DELETE FROM [dbo].[MEC_CAMPAIGN_DETAILS] WHERE MecMonth = '" & DTP_From.Value.Month & "'"
    cmd.ExecuteNonQuery()

End Sub

通过使用此按钮,我正在生成数据:

Private Sub btn_Generate_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btn_Generate.ItemClick

    Dim cmd As New SqlClient.SqlCommand
    cmd.Connection = Connection

    Try
        If Mcount > 0 Then
            If MecMonth = DTP_From.Value.Month Then
                If MsgBox("Do you want to delete and regenerate data for the same month?", MsgBoxStyle.Question & MsgBoxStyle.YesNo, "Confirmation") = MsgBoxResult.No Then Exit Sub
                'cmd = New SqlCommand(" DELETE FROM [dbo].[MEC_CAMPAIGN_DETAILS] WHERE MecMonth = '" & DTP_From.Value.Month & "'", Connection)

                DeleteRecord(DTP_From.Value.Month)
            End If

        Else
            DeleteRecord(DTP_From.Value.Month)
            'cmd = New SqlCommand("DELETE FROM [dbo].[MEC_CAMPAIGN_DETAILS] WHERE MecMonth = '" & DTP_From.Value.Month & "'", Connection)

        End If
    Catch ex As Exception  
        MsgBox(ex.Message)
    End Try

    INSERT_MEC_CAMPAIGN_DETAILS()

End Sub

这是我插入数据的方式:

Dim sql As String
Dim cmd As New SqlClient.SqlCommand
Me.Grid_Data.DataSource = table
'Dim transaction As SqlClient.SqlTransaction

cmd.Connection = Connection
cmd.Transaction = transaction

Try

    For i As Integer = 0 To Grid_Data.Rows.Count - 1
        SERVICEITEMFID = Grid_Data.Rows(i).Cells(0).Value
        C_FullName_Eng = Grid_Data.Rows(i).Cells(1).Value
        C_FullName_Arb = Grid_Data.Rows(i).Cells(2).Value
        C_PhoneNo = Grid_Data.Rows(i).Cells(3).Value
        C_PhoneNo2 = Grid_Data.Rows(i).Cells(4).Value
        Plate_Nbr = Grid_Data.Rows(i).Cells(5).Value
        PlateCode_Desc = Grid_Data.Rows(i).Cells(6).Value
        CodeID = Grid_Data.Rows(i).Cells(7).Value
            SERVICEID = Grid_Data.Rows(i).Cells(8).Value
        COLLECTIONDATE = Grid_Data.Rows(i).Cells(9).Value
        MecMonth = DTP_From.Value.Month

        sql = "INSERT INTO MEC_CAMPAIGN_DETAILS (SERVICEITEMFID ,C_FullName_Eng ,C_FullName_Arb,C_PhoneNo,C_PhoneNo2 ,Plate_Nbr ,PlateCode_Desc ,CodeID,COLLECTIONDATE ,SERVICEID,MecMonth)"
        sql &= " values('" & SERVICEITEMFID & "','" & C_FullName_Eng & "','" & C_FullName_Arb & "','" & C_PhoneNo & "','" & C_PhoneNo2 & "','" & Plate_Nbr & "','" & PlateCode_Desc & "','" & CodeID & "','" & Format(COLLECTIONDATE, "MM/dd/yyyy") & "','" & SERVICEID & "','" & DTP_From.Value.Month & "')"
        cmd = New SqlCommand(sql, Connection)
        transaction = Connection.BeginTransaction(IsolationLevel.Serializable)
        cmd.ExecuteNonQuery()

    Next

    transaction.Commit()
Catch ex As Exception
    transaction.Rollback()
    MsgBox(ex.Message)
End Try
transaction.Dispose()

【问题讨论】:

  • 您是否尝试将cmd.Transaction = transaction 注释掉?
  • @muffi 是的,我确实检查了如何将数据插入数据库
  • 如果要使用事务,正确的方法是:transaction = Connection.BeginTransaction,最后使用transaction.Commit()
  • 发生异常的唯一方法是当您有一个待处理的事务(即没有对您执行的命令调用提交/回滚)然后您尝试执行另一个命令而不将其分配给相同的交易。您必须在每次执行命令时创建一个新事务并提交它,或者传递事务引用并将其分配给每个命令。
  • @soohoonigan 请你检查插入子并告诉我我做错了什么

标签: vb.net executenonquery


【解决方案1】:

您有一个 FOR Loop 用于插入。在循环完成之前,您尝试创建一个新事务,并且前一个事务保持打开状态而不提交。因此,在下一次迭代中,您会收到错误。确保在循环结束时提交。 尝试将cmd.ExecuteNonQuery() 移动到Next 语句下方

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多