使用数据适配器更新 SQL 数据库时发生并发冲突

发布于 2024-08-12 11:03:06 字数 1509 浏览 10 评论 0原文

我在通过数据适配器更新对数据表所做的更改时遇到一些问题。我收到“并发冲突:UpdateCommand 影响了 10 行中的 0 行”

'Get data
Dim Docs_DistributedTable As New DataTable("Docs_Distributed")
Dim sql = "SELECT DISTINCT CompanyID, SortKey, OutputFileID, SequenceNo, DeliveredDate, IsDeliveryCodeCounted, USPS_Scanned FROM Docs_Distributed_Test"

Using sqlCmd As New SqlCommand(sql, conn)
    sqlCmd.CommandType = CommandType.Text
    Docs_DistributedTable.Load(sqlCmd.ExecuteReader)
End Using

'Make various updates to some records in DataTable. 

'Update the Database

Dim sql As String = "UPDATE Docs_Distributed "
sql += "SET DeliveredDate = @DeliveredDate "
sql += "WHERE SequenceNo = @SequenceNo"

Using transaction As SqlTransaction = conn.BeginTransaction("ProcessConfirm")

    Try
        Using da As New SqlDataAdapter
            da.UpdateCommand = conn.CreateCommand()
            da.UpdateCommand.Transaction = transaction
            da.UpdateCommand.CommandText = sql

            da.UpdateCommand.Parameters.Add("@DeliveredDate", SqlDbType.DateTime).SourceColumn = "DeliveredDate"            
            da.UpdateCommand.Parameters.Add("@SequenceNo", SqlDbType.Int).SourceColumn = "SequenceNo"
            da.ContinueUpdateOnError = False
            da.Update(Docs_DistributedTable)
        End Using
        transaction.Commit()
    Catch ex As Exception
        transaction.Rollback()
    End Try
End Using

现在有一个问题。我选择 DISTINCT 记录,基本上每个 SequenceNo 获取一行。可能有很多行具有相同的 SequenceNo,我希望这会更新它们。我不确定这是否与我的问题有关。

I'm having some trouble updating changes I made to a datatable via a dataadapter. I am getting "Concurrency violation: the UpdateCommand affected 0 of 10 rows"

'Get data
Dim Docs_DistributedTable As New DataTable("Docs_Distributed")
Dim sql = "SELECT DISTINCT CompanyID, SortKey, OutputFileID, SequenceNo, DeliveredDate, IsDeliveryCodeCounted, USPS_Scanned FROM Docs_Distributed_Test"

Using sqlCmd As New SqlCommand(sql, conn)
    sqlCmd.CommandType = CommandType.Text
    Docs_DistributedTable.Load(sqlCmd.ExecuteReader)
End Using

'Make various updates to some records in DataTable. 

'Update the Database

Dim sql As String = "UPDATE Docs_Distributed "
sql += "SET DeliveredDate = @DeliveredDate "
sql += "WHERE SequenceNo = @SequenceNo"

Using transaction As SqlTransaction = conn.BeginTransaction("ProcessConfirm")

    Try
        Using da As New SqlDataAdapter
            da.UpdateCommand = conn.CreateCommand()
            da.UpdateCommand.Transaction = transaction
            da.UpdateCommand.CommandText = sql

            da.UpdateCommand.Parameters.Add("@DeliveredDate", SqlDbType.DateTime).SourceColumn = "DeliveredDate"            
            da.UpdateCommand.Parameters.Add("@SequenceNo", SqlDbType.Int).SourceColumn = "SequenceNo"
            da.ContinueUpdateOnError = False
            da.Update(Docs_DistributedTable)
        End Using
        transaction.Commit()
    Catch ex As Exception
        transaction.Rollback()
    End Try
End Using

Now here's the catch. I am selecting DISTINCT records and essentially getting one row per SequenceNo. There may be many rows with the same SequenceNo, and I am hoping this will update them all. I'm not sure if this is related to my problem or not.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过期情话 2024-08-19 11:03:06

您的选择来自“Docs_Distributed_Test”,您的更新是“Docs_Distributed” - 这可能是导致您的问题的原因。序列ID是否相同? (如果不是,那么它的更新可能确实影响了 0 行)。

除此之外,您始终可以在表适配器上禁用乐观并发,它将不再强制验证(尽管在这种情况下,可能不会导致错误,但不会更新任何行)。

Your select is from "Docs_Distributed_Test" and your update is to "Docs_Distributed" - this may be the cause of your issue. Are the sequence ID's the same? (If not then perhaps it is indeed affecting 0 rows with it's update).

Other than that, you can always disable optimistic concurrency on your table-adapter and it will no longer enforce the validation (Though in this case that would likely result in no error but not updating any rows).

倚栏听风 2024-08-19 11:03:06

我不明白 Microsoft 特有的方面,而且 VB 通常很难理解。但这个序列似乎很可疑:

Using transaction As SqlTransaction = conn.BeginTransaction("ProcessConfirm")
    Try
        Using da As New SqlDataAdapter
            da.UpdateCommand = conn.CreateCommand()
            da.UpdateCommand.Transaction = transaction

conn.BeginTransaction 后面跟着 conn.CreateCommand()。这不是a)无用,b)对连接状态有害,或者c)潜在的竞争条件吗?

I don't understand the Microsoft-specific aspects of this, plus VB is often hard to follow. But this sequence seems suspect:

Using transaction As SqlTransaction = conn.BeginTransaction("ProcessConfirm")
    Try
        Using da As New SqlDataAdapter
            da.UpdateCommand = conn.CreateCommand()
            da.UpdateCommand.Transaction = transaction

conn.BeginTransaction is followed by conn.CreateCommand(). Isn't that a) useless, b) hazardous to the connection state, or c) potentially a race condition?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文