并发冲突:更新了预期的 1 条记录中的 0 条

Posted

技术标签:

【中文标题】并发冲突:更新了预期的 1 条记录中的 0 条【英文标题】:concurrency violation : Updated 0 of the expected 1 records 【发布时间】:2016-07-18 15:13:28 【问题描述】:

有人可以帮助我吗?如何摆脱这个错误?

并发违规:更新了预期的 1 条记录中的 0 条

过去几天我一直在为更新命令而苦苦挣扎。我尝试了许多互联网上的解决方案,但我无法解决这个问题。

Imports Microsoft.Office
Imports System.Data.OleDb

Public Class Form1
    Dim dt As New DataTable
    Dim cnn As New OleDb.OleDbConnection

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'filling analyst filter
        Dim sText As String = String.Empty
        Dim sConnString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\ecxx-store\Exxx xxxxsk\Txx Axxx\Enxx Flxxx\Source\Exx_Exxx_Flagging.accdb" 'Put your connection string in here
        Using cn As New OleDb.OleDbConnection(sConnString)
            cn.Open()
            Dim cmd As New OleDb.OleDbCommand("SELECT Distinct Analyst FROM EF ORDER BY Analyst", cn)
            Dim r As OleDb.OleDbDataReader = cmd.ExecuteReader()
            While r.Read()
                ComboBox7.Items.Add(r("analyst"))
            End While
            r.Close()
            cn.Close()
        End Using
        'end of filling analyst filter
        Me.EFTableAdapter.Fill(Me.Exx_Exxxx_ShippingDataset.EF) 
        Me.Label19.Text = "Welcome " & StrConv(Environment.MachineName, vbProperCase)
        Me.Label20.Text = EFBindingSource.Count.ToString() & " entries"
        'Setting Followup Status
        Me.ComboBox8.Text = "Waiting for approval"
        'disabling Second Analyst Details
        Me.txtsecondanalyst.Enabled = False
        Me.txtapproval.Enabled = False
        Me.txtnotes.Enabled = False 
    End Sub

    Private Sub txtdof_ValueChanged(sender As Object, e As EventArgs) Handles txtdof.ValueChanged
        Me.txtnd.Text = DateDiff(DateInterval.Day, CDate(txtdof.Text), Now())
    End Sub

    Private Sub ComboBox7_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox7.SelectedIndexChanged
        If IsNothing(Me.ComboBox8.Text) Then
            EFBindingSource.Filter = "[Analyst] LIKE '%" & ComboBox7.Text & "%'"
        Else
            EFBindingSource.Filter = "[FollowupStatus] LIKE '%" & ComboBox8.Text & "%' AND [Analyst] Like '%" & ComboBox7.Text & "%'"
        End If
        'count of datagrid
        Me.Label20.Text = EFBindingSource.Count.ToString() & " entries"
    End Sub

    Private Sub ComboBox8_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox8.SelectedIndexChanged
        If IsNothing(Me.ComboBox7.Text) Then
            EFBindingSource.Filter = "[FollowupStatus] LIKE '%" & ComboBox8.Text & "%'"
        Else
            EFBindingSource.Filter = "[FollowupStatus] LIKE '%" & ComboBox8.Text & "%' AND [Analyst] Like '%" & ComboBox7.Text & "%'"
        End If
        'count of datagrid
        Me.Label20.Text = EFBindingSource.Count.ToString() & " entries"
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'update
        Me.txtnd.Visible = True
        Me.txtnd.Text = DateDiff(DateInterval.Day, CDate(txtdof.Text), Now())
        On Error GoTo SaveErr
        EFBindingSource.EndEdit()
        EFTableAdapter.Update(Exx_Exxxx_ShippingDataset.EF)
        Me.txtsecondanalyst.Enabled = False
        Me.txtapproval.Enabled = False
        Me.txtnotes.Enabled = False
        MsgBox("Record Updated or Saved")
SaveErr:
    End Sub

    Private Sub Form1_Closed(sender As Object, e As EventArgs) Handles Me.Closed
        EFBindingSource.EndEdit()
        Me.TableAdapterManager1.UpdateAll(Me.Exx_Exxx_ShippingDataset)
    End Sub
End Class

【问题讨论】:

在我看来这来源太多了。考虑写一个MCVE。你也可以这样找到自己的解决方案。 2016 年 - 摆脱 On Error GoTo 好像是vb6转换成vb.net。 On Error GoToMsgBox() 用于 vb6 支持。班级水平Dim 只是伤害了我的眼睛。嘿,至少他有一个 Using 块(尽管有 cn.Close() :)。 【参考方案1】:

根据Concurrency violation: the UpdateCommand affected 0 of the expected 1 records. DB concurrencyException was unhandled

你打电话

Me.EFTableAdapter.Fill(Me.Exx_Exxxx_ShippingDataset.EF)

Form_Load 中,但不能保证在您调用时不会更改行

EFTableAdapter.Update(Exx_Exxxx_ShippingDataset.EF)

Button1_Click

在链接的答案中,

ADO.Net 保留从数据库中选择列时的值。当它执行更新时,条件是您提交时没有任何列发生更改。

也许你应该打电话

dt = dt.GetChanges()

Button1_Click 中更新之前,以便使用数据库中的最新值更新数据表。

【讨论】:

非常感谢@verdolino,我会尝试您的解决方案并更新您。

以上是关于并发冲突:更新了预期的 1 条记录中的 0 条的主要内容,如果未能解决你的问题,请参考以下文章

Devart.Data.MySql:并发冲突:UpdateCommand 影响了预期的 1 条记录中的 0 条

错误:并发冲突:UpdateCommand 影响了预期的 1 条记录中的 0 条

C# System.Data.SQLite:并发冲突:UpdateCommand 影响了预期的 1 条记录中的 0 条

SqlDataAdapter 影响了 1 条预期记录中的 0 条,即使数据集已更改

如何通过存储过程和 DatagridView 更新键填充值?

Mono C# SQL 更新“并发冲突”