问题
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