在Delphi中使用事務

2021-06-28 14:17:50 字數 1993 閱讀 6424

1、直接在delphi中使用事務

procedure tform1.button1click(sender: tobject);

begin

adoconnection1.begintrans;

tryaq2.close;

aq2.sql.clear;

aq2.sql.add('update bank set currentmoney=currentmoney-1 where customername=''張三''');

aq2.execsql;

aq2.sql.clear;

aq2.sql.add('update bank set currentmoney=currentmoney+1 where customername=''李四''');

aq2.execsql;

adoconnection1.committrans;

except

adoconnection1.rollbacktrans;

end;

end;

2、在儲存過程裡使用,然後再呼叫。

procedure tform1.button3click(sender: tobject);

begin

with aq2 do

begin

close;

sql.clear;

sql.add('exec mmm');

tryexecsql;

except 

end;

end;

end;

建立儲存過程 **:

create proc mmm

as begin

/**//*--開始事務--*/

begin transaction

declare @errorsum int    --定義變數,用於累計事務執行過程中的錯誤

set @errorsum=0

update bank set currentmoney=currentmoney-1 where customername='張三'

set @errorsum=@errorsum+@@error    --累計是否有錯誤

update bank set currentmoney=currentmoney+1 where customername='李四'

set @errorsum=@errorsum+@@error --累計是否有錯誤

/**//*--根據是否有錯誤,確定事務是提交還是回滾--*/

if @errorsum<>0

begin

print '交易失敗,回滾事務.'

rollback transaction

endelse

begin

print '交易成功,提交事務,寫入硬碟,永久儲存!'

commit transaction

endprint '檢視轉帳事務後的餘額'

select * from bank

end表:if exists(select* from sysobjects where name='bank')

drop table bank

create table bank

(customername char(10),    --顧客姓名

currentmoney money        --當前餘額)go

/**//*--新增約束,帳戶不能少於元--*/

alter table bank add

constraint ck_currentmoney check(currentmoney>=1)

/**//*--插入測試資料--*/

insert into bank(customername,currentmoney)

select '張三',1000 union

select '李四',1

呵呵。。。以上儲存過程是在網上找的,加以修改的。注:以在查詢器中執行無誤。。資料庫是mssql2000

在Android SQLite中使用事務

使用 sqlitedatabase 的begintransaction 方法可以開啟乙個事務,程式執行到 endtransaction 方法時會檢查 事務的標誌是否為成功,如果程式執行到 endtransaction 之前呼叫了 settransactionsuccessful 方法設定 事務的標誌...

在SQLite中使用事務

用乙個事務轉賬來實現sqlite的事務處理 下面的person類是已經建立好了的表,在 建立資料庫與完成資料添刪改查 一 中可以看到 1 update person set amount amount 10 where personid 1 2 update person set amount am...

SQL中使用事務

begin transaction 開始事務 rollback transaction 提交事務 commit transaction 回滾事務 use pubs declare interrorcode int begin tran update authors set phone 415 354...