回滚事务和提交 [英] Rollback transaction and commit

查看:95
本文介绍了回滚事务和提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅以下代码段。我希望如果AddPersonalRecord因任何原因失败或AddOfficialRecord失败,则必须回滚对数据库所做的更改。我正在使用SQL Server 2005快递版。方法,AddPersonalRecord和AddOfficialRecord,包含插入语句在表中插入记录。



我想要类似的东西:



if(answer ==yes)

{

开始交易();

AddPersonalRecord();

AddOfficialRecord();

}

Please see the following code snippet. I want that if "AddPersonalRecord" fails due to any reason or "AddOfficialRecord" fails, the changes done to the database must be rolled back. I am using SQL Server 2005 express edition. The methods, AddPersonalRecord and AddOfficialRecord, contains Insert Statements to insert record in the table.

I want something like:

if (answer == "yes")
{
Begin Transaction();
AddPersonalRecord();
AddOfficialRecord();
}

推荐答案

您需要使用交易,是:SqlTransaction类 [ ^ ]



It理论上很容易(但我不知道你的软件是如何工作的,所以由你来实现) - 链接包含一个例子。
You do need to use a Transaction, yes: SqlTransaction class[^]

It''s pretty easy in theory (but I have no idea how your software works, so it''s up to you to implement) - the link contains an example.


这篇关于回滚事务和提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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