子表与自动生成的身份密钥

子表与自动生成的身份密钥

本文介绍了如何更新数据集家长和放大器;子表与自动生成的身份密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ADO.NET数据集在我的VB应用程序。我有一个强类型DataSet有一个父表和许多子表。我想生成身份密钥,当我将数据插入父表,然后更新使用相同的密钥的所有子表中的数据(如Foregin键)。

I am using ADO.NET Datasets in my VB Applications. I have a typed dataset with one Parent table and many child tables. I want to generate Identity Key when I insert data into Parent Table and then update the data in all child tables with the Same key (As Foregin key).

最后,我想更新数据库中的数据集(SQL Server08)。

At last, I want to update the dataset in Database(SQL Server08).

好了,上面的东西可以先直接插入父表的数据库成为可能,获得标识列,比使用的子表。

Well, the above thing can be possible by first inserting Parent Table in Database directly, get the Identity column and than use to for Child tables.

不过,我想这是一个自动操作(像LINQ到SQL这需要照顾小学及放大器;在DataContext的外键)

But I want to this as an automatic operation (Like LINQ to SQL which takes care of Primary & Foreign key in datacontext.)

我这样的事情有可能在数据集它负责自动生成列的父母子女表?

I such thing possible in Dataset which takes care of Autogenerated column for Parent and child tables?

谢谢

ABB

推荐答案

几件事情要指出。

  1. 是的,你肯定需要分配给两个表的关系。您可以检查从XSD编辑器(双击您的XSD文件)。默认情况下,关系设定为仅关系不具有任何更新规则。通过进入编辑关系编辑这种关系,并选择外键约束,只有'或'这两个~~~之一。并需要设置更新规则的瀑布! 删除规则是由你。

  1. Yes, you definitely need relations assigned for both tables. You can check from xsd editor (double click your xsd file). By default the relation is set as 'relation only' which doesn't has any 'update rule'. Edit this relation by going into 'edit relation' and select 'Foreign Key Constraint Only' or 'Both~~~' one. And need to set 'Update Rule' as Cascade! 'Delete Rule' is up to you.

现在,当你使用一个新的父表行的ID(自动增加)新的子表行作为外键,你有你在身边使用新的父行的ID之前父行添加到表中的第一。

Now when you use a new parent table row's ID (AutoIncrement) for new child table rows as a foreign key, You have to add the parent row into the table first before you use the new parent row's ID around.

当你调用Update使用的TableAdapter父表,相关的子表的新行都会有正确的parentID自动运行。

As soon as you call Update for the parent table using tableadapter, the associated child table's new rows will have correct parentID AUTOMATICALLY.

我的简单的code片段:

My simple code snippets:

'--- Make Parent Row
Dim drOrder as TOrderRow = myDS.TOder.NewTOrderRow
drOrder.SomeValue = "SomeValue"
myDS.TOrder.AddTOrderRow(drOrder) '===> THIS SHOULD BE DONE BEFORE CHILD ROWS

'--- Now Add Child Rows!!! there are multiple ways to add a row into tables....
myDS.TOrderDetail.AddTOrderDetailRow(drOrder, "detailValue1")
myDS.TOrderDetail.AddTOrderDetailRow(drOrder, "detailvalue2")
'....
'....

'--- Update Parent table first
myTableAdapterTOrder.Update(myDS.TOrder)
'--- As soon as you run this Update above for parent, the new parent row's AutoID(-1)
'--- will become real number given by SQL server. And also new rows in child table will
'--- have the updated parentID

'--- Now update child table
myTableAdapterTOrderDetail.Update(myDS.TOrderDetail)

我希望它能帮助!

I hope it helps!

这篇关于如何更新数据集家长和放大器;子表与自动生成的身份密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 11:52