开发者

Concurrency violation updating a SQL database with a dataadapter

开发者 https://www.devze.com 2022-12-11 21:43 出处:网络
I\'m having some trouble updating changes I made to a datatable via a dataadapter. I am getting \"Concurrency violation: the UpdateCommand affec开发者_开发知识库ted 0 of 10 rows\"

I'm having some trouble updating changes I made to a datatable via a dataadapter. I am getting "Concurrency violation: the UpdateCommand affec开发者_开发知识库ted 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.


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).


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消