C# DataSet, DataAdapter — How to change row state to invoke an INSERT?

怎甘沉沦 提交于 2019-12-12 15:43:38

问题


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.

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.

EDIT: Just in case, DataRelations have already been set. Normal INSERT, UPDATE, DELETE and SELECT works perfectly.


回答1:


maybe this works

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


来源:https://stackoverflow.com/questions/4582380/c-sharp-dataset-dataadapter-how-to-change-row-state-to-invoke-an-insert

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!