简体   繁体   English

C#DataSet,DataAdapter - 如何更改行状态以调用INSERT?

[英]C# DataSet, DataAdapter — How to change row state to invoke an 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. 我正在尝试实现“另存为”按钮以获取DataAdapter.Filled()的DataSet中的数据,并将INSERT导入数据库。

The DataSet has 4 tables -- 1 parent (single row table), 3 child. DataSet有4个表 - 1个父表(单行表),3个子表。 with foriegn cascade delete/update constraints. 与foriegn级联删除/更新约束。 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; 我的目的是要求用户提供一个新的主键(复杂),然后尝试告诉DataAdapter或DataSet将4个表中的所有行(以及后续的新行)标记为DataRowState.Added; But DataRow.SetAdded() throws exception "Can only SetAdded on DataRowState.Unchanged rows" 但DataRow.SetAdded()抛出异常“只能在DataRowState.Unchanged行上设置SetAdded”

Anyone has idea how to do it? 任何人都知道如何做到这一点? Any other efficient methods to do a "Save As" is also welcomed. 任何其他有效的方法来做“另存为”也受到欢迎。 Many thanks. 非常感谢。

EDIT: Just in case, DataRelations have already been set. 编辑:以防万一,DataRelations已经设置好了。 Normal INSERT, UPDATE, DELETE and SELECT works perfectly. 正常INSERT,UPDATE,DELETE和SELECT工作正常。

maybe this works 也许这有效

row.AcceptChanges(); // sets DataRowState.Unchanged
row.SetAdded();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM