且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何一次将记录插入多个表中

更新时间:2022-01-03 22:14:59

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

如果没有,那么在你的普通代码中使用一个事务。 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.