如何通过关系插入父表和子表 [英] How to insert into parent and child tables with relation

查看:107
本文介绍了如何通过关系插入父表和子表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2张桌子,父母和孩子.
表之间存在一对一的DataRelationship.

我已经为两个表的所有字段设置了一个带有详细信息文本框的Windows窗体. (因为关系是一对一的,所以我不需要为子记录添加gridvie).

我已经进行了所有设置,以便可以在ParentBindingSource中移动,并且所有子字段都进行了适当调整以根据上下文进行显示.

问题是我想同时添加新的父级和子级记录.

我有一个ADDButton,它调用"ParentBindingSource.AddNew()"
当我这样做时,我的自动增量ParentID字段显示值为-1;
子字段为空白;

我有一个调用"ParentTableAdapter.Update()"的SAVEButton.
这样可以保存新的Parent记录.

问题:我无法确定事件的顺序以同时添加新的子记录!

我想念什么???

谢谢

I have 2 tables, Parent and Child.
There is a one-to-one DataRelationship between the tables.

I have set up a windows form with detail text boxes for all the fields of both tables. (since the relationship is one-to-one I didn''t need a gridvie for the child records).

I have everything set up so that I can move through the ParentBindingSource and all the child fields adjust appropriately to display according to context.

The problem is that I want to Add new Parent AND Child records at the same time.

I have an ADDButton which calls "ParentBindingSource.AddNew()"
When I do this, my autoincrement ParentID field shows a value of -1;
The Child fields go blank;

I have a SAVEButton which calls "ParentTableAdapter.Update()"
This works to save the new Parent record.

PROBLEM: I cannot figure out the sequence of events to add a NEW child record at the same time!

What am I missing???

Thanks

推荐答案

将数据插入到数据表中时,未设置PrimaryKey值.在子表中需要此值作为对父表的引用.因此,首先将数据添加到父表.找到最新的PK值,并在子表的插入内容中使用该值.
When you INSERT data into a datatable the PrimaryKey Value is not set. This value is needed in the child table as a reference to the parent table. So first add data to parent table. Find the newest PK value and use that value in your insert in your child table.


下面是我的代码.
我的测试非常基础.
用户按下新建"按钮,然后尝试创建父记录和子记录.
我在表格上输入数据.
我单击保存"按钮,然后尝试保存数据.

我认识到需要获取新的KeyID来保存到子级的问题,但是问题是当我尝试取回添加的子级行时,什么也没有...

我相信单击新建"按钮时一定会丢失某些东西...当我将记录添加到ChildBindingSource时,它可能没有链接到父记录吗?有没有编程方式来"AddChildRecord"?


私人无效New_Click(对象发送者,EventArgs e)
{
ParentBindingSource.AddNew();
ChildBindingSource.AddNew();
}

私人void SaveBTN_click(object sender,EventArgs e)
{
ParentBindingSource.EndEdit();
ChildBindingSource.EndEdit();
foreach(dataSet11.ParentTable中的DataSet1.ParentRow cr)
{
如果(cr.RowState == DataRowState.Added)
{

/*
调用GetChildRows不会返回任何值!!! ???
*/
DataRow [] childRows = cr.GetChildRows(dataSet11.Relations ["Parent_ChildRelation"]);
ParentTableAdapter.Update(cr);
ChildTableRow childRow =(ChildTableRow)childRows [0];
childRow.KeyID = cr.KeyID;
ChildTableAdapter.Update(childRow);

}
}
Below is my code.
My test is very basic.
The user presses the "New" button and I try to create both a Parent and Child record.
I enter the data on the Form.
I click the "Save" button and try to save the data.

I recognize the need to get the new KeyID to save to the child, but the problem is that when I try to get the child row back that I added, nothing is there...

I believe I must be missing something when I click the New button... When I add the record to the ChildBindingSource perhaps it doesn''t get linked to the parent record? Is there a programmatic way to "AddChildRecord"?


private void New_Click(object sender, EventArgs e)
{
ParentBindingSource.AddNew();
ChildBindingSource.AddNew();
}

private void SaveBTN_click( object sender, EventArgs e)
{
ParentBindingSource.EndEdit();
ChildBindingSource.EndEdit();
foreach (DataSet1.ParentRow cr in dataSet11.ParentTable)
{
if (cr.RowState == DataRowState.Added)
{

/*
The call to GetChildRows doesn''t return any values!!! ???
*/
DataRow[] childRows = cr.GetChildRows(dataSet11.Relations["Parent_ChildRelation"]);
ParentTableAdapter.Update(cr);
ChildTableRow childRow = (ChildTableRow)childRows[0];
childRow.KeyID = cr.KeyID;
ChildTableAdapter.Update(childRow);

}
}


这篇关于如何通过关系插入父表和子表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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