Operations:
Delete in DataGridView selected row from Dataset:
从Dataset中删除DataGridView选定的行:
FuDataSet.FuRow row = (FuDataSet.FuRow) ((DataRowView)FuBindingSource.Current).Row;
row.Delete();
To add a new Row I'm doing:
要添加我正在做的新行:
FuDataSet.FuRow row = FuDataSet.Fus.NewFuRow();
row.Someting = "Some initial Content";
row.SomethingElse = "More Initial Content";
...
FuDataSet.Fus.AddFuRow(row);
Saving user changes in current row in Dataset:
在数据集中保存当前行中的用户更改:
FuDataSet.FuRow row = (FuDataSet.FuRow) (((DataRowView) FuBindingSource.Current).Row);
row.Someting = someTextBox.text;
...
Save in Database:
保存在数据库中:
Validate();
FuBindingSource.EndEdit();
FuTableAdapter.Update(FuDataSet.Fus); <-- Exception here
I'm using the standard DatagridView, Dataset, TableAdapter, BindingSource Scheme VS puts automaticly up after defining the database structure. There is only a single table involved and SQL Server compact 3.5 is used.
我正在使用标准的DatagridView,Dataset,TableAdapter,BindingSource Scheme VS在定义数据库结构后自动启动。只涉及一个表,并使用SQL Server compact 3.5。
Now my problem is that I get a Concurrency Exception (DeletedRowInaccessibleException) each time I'm doing this (starting with an empty database): Creating a new row, delete this row, save in Database, new row, save in database, delete this row, save in database <- Exception
现在我的问题是我每次执行此操作时都会得到并发异常(DeletedRowInaccessibleException)(从空数据库开始):创建新行,删除此行,保存在数据库中,新行,保存在数据库中,删除此行,保存在数据库中< - 异常
I think that there is some synchroniszing problem between the database and the dataset.
我认为数据库和数据集之间存在一些同步问题。
If I'm reloading the databse after each save via FuTableAdapter.Fill(FuDataSet.Fus) the problem is gone. However, this cannot be the intention I think.
如果我在每次保存后通过FuTableAdapter.Fill(FuDataSet.Fus)重新加载数据库,问题就消失了。但是,这不是我想的意图。
I hope someone can help me out and spot a failure in the design or explain me what may go wrong.
我希望有人可以帮助我,发现设计失败或解释我可能出错的地方。
Thank you!
2 个解决方案
#1
Does your table have an auto increment identity column as the primary key? If so it might not be updating the dataset table with the new value after the insert, so when you come to delete it, it cannot find the row in the database. That could explain why it works once you called the Fill() method.
您的表是否有自动增量标识列作为主键?如果是这样,它可能不会在插入后使用新值更新数据集表,因此当您要删除它时,它无法在数据库中找到该行。这可以解释为什么一旦你调用Fill()方法它就可以工作。
You will need to somehow return the primary key on the insert so that the dataset table stays in sync with database. If you are using a store procedure to do the inserts, then primary key can be returned using an out parameter. Not sure what the best way is if you are using an SQL insert statement in the command, but you will then have to get the primary key back from the database table and assign it to the database table row.
您将需要以某种方式返回插入的主键,以便数据集表保持与数据库同步。如果使用存储过程执行插入,则可以使用out参数返回主键。如果在命令中使用SQL insert语句,则不确定最佳方法是什么,但是您必须从数据库表中获取主键并将其分配给数据库表行。
Not sure if you are doing this after the saveing, but calling FuDataSet.AcceptChanges() will help the dataset track new changes after the database has been updated.
保存后不确定是否这样做,但调用FuDataSet.AcceptChanges()将帮助数据集在数据库更新后跟踪新的更改。
#2
What you have listed there is correct. When a new row is created in the dataset table, it creates it's own ID. When you save to the database, the database table creates it's own ID as well, which in most cases will be different to the one in the dataset.
您列出的内容是正确的。在数据集表中创建新行时,它会创建自己的ID。保存到数据库时,数据库表也会创建自己的ID,这在大多数情况下会与数据集中的ID不同。
When you created the table adapter for that table, you had to supply a sql state to create the dataset table. On the advanced Options button, there is a checkbox called "Refresh the data table". Check that to have a sql statement added after the insert and update to retrieve the identity column.
为该表创建表适配器时,必须提供sql状态才能创建数据集表。在高级选项按钮上,有一个名为“刷新数据表”的复选框。检查是否在插入和更新后添加了sql语句以检索标识列。
If the checkbox is disabled then I am not sure what else you could, other than reload the data after each save, which will not be optimal.
如果禁用该复选框,那么除了在每次保存后重新加载数据之外,我不确定您还能做什么,这不是最佳选择。
Sorry I cannot be of more assistance. Best of luck
对不起,我无法提供更多帮助。祝你好运
#1
Does your table have an auto increment identity column as the primary key? If so it might not be updating the dataset table with the new value after the insert, so when you come to delete it, it cannot find the row in the database. That could explain why it works once you called the Fill() method.
您的表是否有自动增量标识列作为主键?如果是这样,它可能不会在插入后使用新值更新数据集表,因此当您要删除它时,它无法在数据库中找到该行。这可以解释为什么一旦你调用Fill()方法它就可以工作。
You will need to somehow return the primary key on the insert so that the dataset table stays in sync with database. If you are using a store procedure to do the inserts, then primary key can be returned using an out parameter. Not sure what the best way is if you are using an SQL insert statement in the command, but you will then have to get the primary key back from the database table and assign it to the database table row.
您将需要以某种方式返回插入的主键,以便数据集表保持与数据库同步。如果使用存储过程执行插入,则可以使用out参数返回主键。如果在命令中使用SQL insert语句,则不确定最佳方法是什么,但是您必须从数据库表中获取主键并将其分配给数据库表行。
Not sure if you are doing this after the saveing, but calling FuDataSet.AcceptChanges() will help the dataset track new changes after the database has been updated.
保存后不确定是否这样做,但调用FuDataSet.AcceptChanges()将帮助数据集在数据库更新后跟踪新的更改。
#2
What you have listed there is correct. When a new row is created in the dataset table, it creates it's own ID. When you save to the database, the database table creates it's own ID as well, which in most cases will be different to the one in the dataset.
您列出的内容是正确的。在数据集表中创建新行时,它会创建自己的ID。保存到数据库时,数据库表也会创建自己的ID,这在大多数情况下会与数据集中的ID不同。
When you created the table adapter for that table, you had to supply a sql state to create the dataset table. On the advanced Options button, there is a checkbox called "Refresh the data table". Check that to have a sql statement added after the insert and update to retrieve the identity column.
为该表创建表适配器时,必须提供sql状态才能创建数据集表。在高级选项按钮上,有一个名为“刷新数据表”的复选框。检查是否在插入和更新后添加了sql语句以检索标识列。
If the checkbox is disabled then I am not sure what else you could, other than reload the data after each save, which will not be optimal.
如果禁用该复选框,那么除了在每次保存后重新加载数据之外,我不确定您还能做什么,这不是最佳选择。
Sorry I cannot be of more assistance. Best of luck
对不起,我无法提供更多帮助。祝你好运