C#DataSet,DataAdapter-如何更改行状态以调用INSERT? [英] C# DataSet, DataAdapter -- How to change row state to invoke an INSERT?

查看:97
本文介绍了C#DataSet,DataAdapter-如何更改行状态以调用INSERT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现另存为按钮,以将DataAdapter.Filled()中的数据集中的数据插入到数据库中。

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.

数据集有4个表-1个父表(单行表),3个子表。具有foriegn级联删除/更新约束。我的意图是向用户询问新的主键(复杂键),然后尝试告诉DataAdapter或DataSet将4个表中的所有行(以及随后的新行)标记为DataRowState.Added;。但是DataRow.SetAdded()引发异常只能在DataRowState.Unchanged行上使用SetAdded。

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆