问题描述
我正在尝试实现一个另存为"按钮来获取 DataSet 中的数据,该数据是 DataAdapter.Filled(),然后 INSERT 到数据库中.
I am trying to implement a "Save As" button to take the data in a DataSet that was DataAdapter.Filled(), and INSERT into the database.
DataSet 有 4 个表——1 个父表(单行表),3 个子表.具有外部级联删除/更新约束.我的意图是向用户询问新的主键(复杂),然后尝试告诉 DataAdapter 或 DataSet 将 4 个表中的所有行(以及后续的新行)标记为 DataRowState.Added;但是 DataRow.SetAdded() 抛出异常Can only SetAdded on DataRowState.Unchanged rows"
The DataSet has 4 tables -- 1 parent (single row table), 3 child. with foriegn cascade delete/update constraints. My intention is to ask the user for a new primary key (complex) and then try to tell the DataAdapter or DataSet to mark all the rows (and subsequent new ones) in the 4 tables as DataRowState.Added; But DataRow.SetAdded() throws exception "Can only SetAdded on DataRowState.Unchanged rows"
有人知道怎么做吗?也欢迎任何其他有效的另存为"方法.非常感谢.
Anyone has idea how to do it? Any other efficient methods to do a "Save As" is also welcomed. Many thanks.
以防万一,DataRelations 已经设置好了.正常的 INSERT、UPDATE、DELETE 和 SELECT 可以完美运行.
Just in case, DataRelations have already been set. Normal INSERT, UPDATE, DELETE and SELECT works perfectly.
推荐答案
也许这行得通
row.AcceptChanges(); // sets DataRowState.Unchanged
row.SetAdded();
这篇关于C# DataSet、DataAdapter -- 如何更改行状态以调用 INSERT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!