插入父子关系 [英] Insert in parent child relation

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

问题描述

大家好,



我在父子表中有一对多的关系。我想在单个事务中插入记录是两个表。如果因为任何原因没有插入子表记录,那么父记录也需要回滚。



我们如何使用asp.net在业务层实现这一点。 />


Thanx&Regards

Santosh

Hi All,

I have one to many relation in parent child table. I want to insert records is both table in single transaction. If because of any reason child table records not get inserted then parent record also need to rollback.

How would we achieve this in business layer using asp.net.

Thanx & Regards
Santosh

推荐答案

最好的方法是要将它创建为存储过程,但无论您是这样做还是内联代码,您真正需要做的是创建一个事务,您可以在两个插入成功后提交,或者如果失败则返回。



你究竟是怎么做的,取决于内联或SP,以及你选择的语言,但对于SP,请参见此处:http://technet.microsoft.com/en-us/library/ms188929.aspx [ ^ ]和C#见这里: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction.aspx [ ^ ]
The best way to do this is to create it as a stored procedure, but whether you do that or inline the code what you really need to do is create a Transaction, which you either Commit after both inserts have succeeded, or Rollback if either fails.

Exactly how you do that will depend on in-line or SP, and the language you choose, but for SP see here: http://technet.microsoft.com/en-us/library/ms188929.aspx[^] and for C# see here: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction.aspx[^]


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

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