如何一次将记录插入多个表中 [英] how to insert records into multiple table at one time

查看:76
本文介绍了如何一次将记录插入多个表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在多个表中插入记录,但是假设我有两个表,一个有主键,另一个有外键,一次将记录插入这两个表.plz帮帮我?

i want insert record in multiple table but suppose i have two table one have primary key another have foreign key and insert record into this two table at one time.plz help me?

推荐答案

最好的方法是创建一个存储过程,如果可以 - 并在其中使用一个事务。

如果没有,那么在你的普通代码中使用一个事务。 br />
但是在任何一种情况下,过程都是相同的:首先写入主键表项,然后使用主键表中的新数据写入外键项。如果任何一个失败,你回滚事务,否则你提交它。



我无法提供代码的确切细节,因为它对于存储过程会有所不同,或VB或C#...但谷歌无论如何都会帮助你。
The best way is to create a stored procedure, if you can - and use a transaction within that.
If not, then use a Transaction in your "normal" code.
But the procedure is the same in either case: write the Primary key table entry first, then write the Foreign key entry using the new data from the Primary key table. If either fails, you rollback the transaction, otherwise you commit it.

I can't give exact details on the code, because it will be different for a stored procedure, or VB or C#...but Google will help you with the parts anyway.


这篇关于如何一次将记录插入多个表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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