DBConcurrencyException。 Sqlite TableAdapter使用UpdateCommand而不是InsertCommand

时间:2017-04-23 13:55:49

标签: sql database vb.net sqlite dataset

        Dim newRow As wilsysFixedDataSet.FormulaRow =
            blend.formulaDataSet.Formula.NewFormulaRow

在设置新行的所有属性之后的几行......

        Console.WriteLine("Current Row State Before Added: {0}", newRow.RowState)

        blend.formulaDataSet.Formula.AddFormulaRow(newRow)

        Console.WriteLine("Current Row State After Added: {0}", newRow.RowState)

        Console.WriteLine(blend.formulaDataSet.Formula.Contains(newRow))

        blend.formulaTableAdapter.Update(blend.formulaDataSet.Formula)

我在尝试使用Form创建新的Formula记录时遇到问题,将该行添加到FormulaDataTable,然后使用FormulaDataTable作为参数调用FormulaDataSetTableAdapter.update()方法。我确保行的id属性不存在,并且设置了所有其他必需的属性,并且该行在dataTable中不存在。 出于某些STRANGE原因,TableAdapter选择调用UPDATECOMMAND,如下面的堆栈跟踪中所示,它应该插入。 为什么这个以及如何让它用新行调用适当的方法。

这里我正在打印到控制台:   - 在将newRow添加到DataTable之前的newRow.RowState   - 将newRow添加到DataTable后的newRow.RowState   - DataTable.Contains(newRow)   - 列出项目

Current Row State Before Added: Detached
Current Row State After Added: Added
True 

下面我在执行时收到以下StackTrace DataSetTableAdapter.update(FormulaDataTable):

System.Data.DBConcurrencyException: Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.
   at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
   at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
   at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
   at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
   at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
   at RepackWPF.wilsysFixedDataSetTableAdapters.FormulaTableAdapter.Update(FormulaDataTable dataTable)
   at RepackWPF.NewFormDialog.NewFormula()

1 个答案:

答案 0 :(得分:0)

原来我能够通过"刷新"来解决这个问题。通过调用

更改FormulaDataSet(包含tableadapters的主数据集)上的记录
FormulaDataSet.AcceptChanges()

方法。我必须在FormulaDataSet上的每次更新调用之前调用它。 这将RowState从Modified设置为Unchanged,并允许事务通过。

相关问题