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

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

问题描述

我正在尝试实现一个另存为"按钮来获取 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)