English 中文(简体)
Concurrency violation updating a SQL database with a dataadapter
原标题:

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.

最佳回答

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?





相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签