SQLhelper使用事務

2021-06-06 12:20:10 字數 1297 閱讀 4611

1、宣告連線myconnectstring 

2、開啟事務begintransaction

3、執行時使用st

4、成功提交,失敗回滾  

public function testshiwu()

dim strsql as string

dim myconnectstring as string = o.getconn

dim myconnection as new sqlconnection(myconnectstring)

myconnection.open()

dim st as sqltransaction = myconnection.begintransaction()

try'插入使用者

strsql = "insert into ec_user(cuserid,cusername,cpassword,cdepcode,badministrator,buse,bdel,cdyzm,cip,cdemo) "

strsql += "values('test','test','test','01',0,0,0,'t','','')"

sqlhelper.executenonquery(st, commandtype.text, strsql)

'更新使用者

strsql = "update ec_user set cusername='aaa' where cuserid='test' "

sqlhelper.executenonquery(st, commandtype.text, strsql)

'再次插入使用者,違反主鍵約束,事務回滾

strsql = "insert into ec_user(cuserid,cusername,cpassword,cdepcode,badministrator,buse,bdel,cdyzm,cip,cdemo) "

strsql += "values('test','test','test','01',0,0,0,'t','','')"

sqlhelper.executenonquery(st, commandtype.text, strsql)

st.commit()

return "success"

catch ex as exception

st.rollback()

msgbox(ex.message, msgboxstyle.critical, "發生錯誤")

return ""

finally

myconnection.close()

end try

end sub

Ibatis使用事務

一 事務概念 事務是一種機制 是一種操作序列,它包含了一組資料庫操作命令,這組命令要麼全部執行,要麼全部不執行。因此事務是乙個不可分割的工作邏輯單元。在資料庫系統上執行併發操作時事務是作為最小的控制單元來使用的。這特別適用於多使用者同時操作的資料通訊系統。例如 訂票 銀行 保險公司以及 交易系統等。...

SpringBoot使用事務

事務是很多專案中需要注意的東西,有些場景如果沒有加事務控制就會導致一些髒資料進入資料庫,本文簡單介紹springboot怎樣使用事務。本文使用的是之前整合jpa的文章,具體可以參考 傳送門。無論是配置還是pom檔案等等,沒有任何改變,改變的是測試的controller上面的兩個方法,其中乙個方法使用...

SQL使用事務

begin transaction 開始事務 declare errorsun int 定義錯誤計數器 set errorsun 0 沒錯為0 update a set id 232 where a 1 事務操作sql語句 set errorsun errorsun error 累計是否有錯 upd...