如何插入多行(一对多关系) [英] How to insert multiple rows into (one to many relation)

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

问题描述

我有两个表由一对多关系链接,

第一个表是(Bill's),第二个是(Bill详情)



来自C#WinForms我想在Bills表中插入一条记录,并将多条记录插入到Details表中

使用存储过程和WindowsForm照常发送的参数



我尝试了什么:



i试图使用sqlcommand将单个记录插入账单表

然后使用for循环执行另一个sqlcommand在详细信息表中插入多个记录链接到上一个表(一对多)关系

但是有没有更好的方法要做那个还是那个唯一的方法?

I've two tables linked by one to many relation,
first table is (Bill's) and the second is (Bill details)

From C# WinForms i want to insert one record into Bills table and multiple records into Details table
using a stored procedure and parameters sent from the WindowsForm as usual

What I have tried:

i tried to use a sqlcommand to insert single record into "bills table"
then use a "for" loop to execute another sqlcommand to insert multiple records in details table linked to the previous table(One to many) relation
But is there a better way to do that or that's the only way?

推荐答案

这就是唯一的方法。 Bill记录必须首先获取其ID,然后将其包含在Details记录(外键)中。如果您要使用具有外键的记录(相关ID在单独的表中),则必须首先存在外部记录。
That's just about the only way to do it. The Bill record has to go first, get its ID, then you include that in the Details records (foreign key). If you're going to have records that have a foreign key (related ID in a separate table), the foreign record has to exist first.


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

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