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

时间:2017-07-10 12:32:12

标签: vb.net executenonquery

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

错误是:

  

当分配给命令的连接处于挂起的本地事务中时,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()

1 个答案:

答案 0 :(得分:0)

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

下方的Next