具有多个表的 C# DataAdapter 和 DataSet [英] C# DataAdapter and DataSet with multiple table

查看:23
本文介绍了具有多个表的 C# DataAdapter 和 DataSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从很多地方了解到可以使用 DataAdapter 用多个表填充 DataSet.它也没有说明单个 Update 调用是否可以更新 DataSet 中的所有表.

I have read from many places that it is possible to fill a DataSet with multiple tables using a DataAdapter. It also does not say whether a single Update call can update all the tables in the DataSet.

谁能帮我弄清楚如何做到这一点?

Can someone help me figure out how this can be done?

除了在第二次填充之前更改 DataAdapter 上的 SelectCommand 之外,似乎没有任何(我尝试在网上查找)示例说明如何执行此操作.但我觉得这种方法违背了 DataAdapter 的目的.

It seems like there isn't any ( i tried finding online ) examples on how to do it except for one that changes the SelectCommand on the DataAdapter before the second fill. But I feel this method defeats the purpose of the DataAdapter.

据我所知,也许单个 DataAdapter 只能处理单个数据库表,而 Update 仅适用于该表.因此,多表 DataSet 将需要各自的 DataAdapter 调用它们的 Update 来完全更新 DataSet.是这样吗?

From what I figure, perhaps a single DataAdapter can only handle a single database table and Update only works on that table. Hence a multi-table DataSet will require respective DataAdapters call their Update to fully update the DataSet. Is this the case?

最后,外键关系和约束是否会自动保存在 DataSet 中(级联删除、级联更新)?

Finally, will foreign key relations and contraints hold in a DataSet (cascade delete, cascade update) automatically?

也许一个示例或教程的链接可能会有所帮助.非常感谢!

Maybe a link to an example or tutorial might help. Many thanks!

推荐答案

  1. 是的,单表的单适配器.但是

  1. Yes it is true that, Single Adapter for single table.But

您可以使用使用表适配器管理器一次保存所有适配器,表适配器管理器可以有许多单独的适配器,您可以调用全部保存.喜欢,所以不需要多次调用 save 它也有其他功能.

You can use Use table adapter manager for saving all at once, table adapter manager can have many individual adapters and you can call save for all. like, So no need to call save multiple time also it has other features too.

public void SaveWithManager()
{
DataSet1TableAdapters.TableAdapterManager mgr1 = new DataSet1TableAdapters.TableAdapterManager();
DataSet1TableAdapters.Table1TableAdapter taTbl1 = new DataSet1TableAdapters.Table1TableAdapter();
DataSet1TableAdapters.Table2TableAdapter taTbl2 = new DataSet1TableAdapters.Table2TableAdapter();


mgr1.Table1TableAdapter = taTbl1;
mgr1.Table2TableAdapter = taTbl2;
mgr1.UpdateOrder = DataSet1TableAdapters.TableAdapterManager.UpdateOrderOption.InsertUpdateDelete; 
mgr1.UpdateAll(your dataset);

}

最终级联更新删除在数据集中处理.您可以查看关系的属性和级联的各种选项.(类型化数据集)

Finally cascade update delete is handled in dataset. You can view properties of relation and various options for cascade.(Typed dataset)

这篇关于具有多个表的 C# DataAdapter 和 DataSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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