SQL插入主要和详细记录 [英] SQL Insert Main and Details Records

查看:90
本文介绍了SQL插入主要和详细记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表''采购订单''abd''采购订单产品''。



我使用.NET c#程序调用存储过程。



我想首先为''采购订单'生成id和插入记录。然后插入采购订单产品的记录,因为它包含生成的ID。



如果只调用单个存储过程,我可以使用TRY CATCH和ROLLBACK来确保两个表的更新同步。但是,我不知道如何将信息列表(即采购订单产品的所有记录)传递到单个商店程序中。



例如将以下列表作为参数传递

每个产品包括名称,数量,金额

ProductA,1,$ 20

ProductB,3,$ 28

...



所以我假设每次为一个产品调用insert product程序,但是它不能回滚''Purchase的插入行如果在插入''产品'时发生任何错误,请订购'。



是否有任何可以解决我的情况的做法,以便我可以确保所有记录(订购和产品)将被添加或者不会添加任何记录并且错误已知?

I got two Tables ''Purchase Order'' abd ''Purchase Order Products''.

I am using .NET c# program to call store procedure.

I would like to generate id and insert record for ''Purchase Order'' first. Then insert records for ''Purchase Order Products'' since it includes the ids generated.

If just calling a single store procedure, I can use TRY CATCH and ROLLBACK to make sure updates of two tables in sync. However, I do not know how to pass a list of information (i.e. all records for Purchase Order Product) into a single store procedure.

e.g. pass the following list as parameters
each Product include name, qty, amount
ProductA, 1, $20
ProductB, 3, $28
...

So I assume calling insert product procedure each time for one product, but then it cannot rollback the inserted row of ''Purchase Order'' if any errors occur in inserting ''Products''.

Is there any practice that can solve my situation so I could make sure all records (Order and Products) will be added or no records will be added and error is known?

推荐答案

20

ProductB,3,
20
ProductB, 3,


28

...



所以我假设每次为一个产品调用insert产品程序,但是如果在插入产品时出现任何错误,它就无法回滚插入的采购订单行。



是否有任何可以解决我的情况的做法所以我可以确保添加所有记录(订单和产品)或不添加任何记录并且错误已知?
28
...

So I assume calling insert product procedure each time for one product, but then it cannot rollback the inserted row of ''Purchase Order'' if any errors occur in inserting ''Products''.

Is there any practice that can solve my situation so I could make sure all records (Order and Products) will be added or no records will be added and error is known?


您好,

您需要的解决方案研究在ADO.NET中使用事务 [ ^ ]并阅读SqlTransaction类 [ ^ ]。经过研究,您可以将这些给定的示例代码集成到您的系统中。请查看以下链接。



http://code.rumeryenterprises.com/post/2010/03/14/C-Sql-Transaction-Example.aspx [ ^ ]

http://www.aspnettutorials.com/tutorials/database/sql-transaction-csharp.aspx [ ^ ]

http://www.dotnetspider.com/resources/31532-Sql-Transaction.aspx [ ^ ]
Hi,
For your solution you need to study Using Transactions in ADO.NET[^] and read about SqlTransaction Class[^]. After study this you are able to integrate such given example''s code into your system. Please review below links.

http://code.rumeryenterprises.com/post/2010/03/14/C-Sql-Transaction-Example.aspx[^]
http://www.aspnettutorials.com/tutorials/database/sql-transaction-csharp.aspx[^]
http://www.dotnetspider.com/resources/31532-Sql-Transaction.aspx[^]


这篇关于SQL插入主要和详细记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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