国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

.net 中的事務總結

2024-07-10 13:05:00
字體:
來源:轉載
供稿:網友


1>存儲過程級別的事務。
create procedure addinfo
(@studentname varchar(20),....)
as
begin transaction
insert ......
insert....
if .....
rollback transaction
update.....
.....
commit transaction
注:可以在存儲過程中使用save transaction選擇回滾的位置

2>數據庫級別的事務處理。
需要導入imports system.data.sqlclient名稱空間。

'this function will add student's infomation and its parent's information concurrently !
'so we should use transaction !
public shared function insertinfo(byval student as clsstudent, byval parent as clsparent) as boolean

dim success as boolean = true
dim cmdstudent as new sqlcommand("insert into student(name,sex,classname) values(@name,@sex,@classname)", cnn)
dim cmdparent as new sqlcommand("insert into parent(name,sex,salary) values(@name,@sex,@salary)", cnn)
dim cmdgetstudentid as new sqlcommand("select studentid from student where [email protected] ", cnn)
dim cmdgetparentid as new sqlcommand("select parentid from parent where [email protected]", cnn)
dim cmdstudentparent as new sqlcommand("insert into studentparent(studentid,parentid)values(@studentid,@parentid)", cnn)

cmdstudent.parameters.add("@name", student.name)
cmdstudent.parameters.add("@sex", student.sex)
cmdstudent.parameters.add("@classname", student.classname)

cmdparent.parameters.add("@name", parent.name)
cmdparent.parameters.add("@sex", parent.sex)
cmdparent.parameters.add("@salary", parent.salary)

cmdgetstudentid.parameters.add("@name", student.name)

cmdgetparentid.parameters.add("@name", parent.name)

dim transaction as sqltransaction

try
cnn.open()
transaction = cnn.begintransaction
cmdstudent.transaction = transaction
cmdparent.transaction = transaction
cmdgetstudentid.transaction = transaction
cmdgetparentid.transaction = transaction
cmdstudentparent.transaction = transaction
dim studentid, parentid as integer

cmdstudent.executenonquery()
cmdparent.executenonquery()
studentid = cmdgetstudentid.executescalar
parentid = cmdgetparentid.executescalar
cmdstudentparent.parameters.add("@studentid", studentid)
cmdstudentparent.parameters.add("@parentid", parentid)
cmdstudentparent.executenonquery()

transaction.commit()

catch ex as exception
transaction.rollback()
success = false
messagebox.show(ex.message)
finally

cnn.close()
end try
return success


end function

3>頁面級別的事務處理,也稱com級別的事務。
需要導入imports system.data.sqlclient和imports system.enterpriseservices


'this function will add student's infomation and its parent's information concurrently !
'so we should use transaction !
public shared function insertinfo(byval student as clsstudent, byval parent as clsparent) as boolean

dim success as boolean = true
dim cmdstudent as new sqlcommand("insert into student(name,sex,classname) values(@name,@sex,@classname)", cnn)
dim cmdparent as new sqlcommand("insert into parent(name,sex,salary) values(@name,@sex,@salary)", cnn)
dim cmdgetstudentid as new sqlcommand("select studentid from student where [email protected] ", cnn)
dim cmdgetparentid as new sqlcommand("select parentid from parent where [email protected]", cnn)
dim cmdstudentparent as new sqlcommand("insert into studentparent(studentid,parentid)values(@studentid,@parentid)", cnn)

cmdstudent.parameters.add("@name", student.name)
cmdstudent.parameters.add("@sex", student.sex)
cmdstudent.parameters.add("@classname", student.classname)

cmdparent.parameters.add("@name", parent.name)
cmdparent.parameters.add("@sex", parent.sex)
cmdparent.parameters.add("@salary", parent.salary)

cmdgetstudentid.parameters.add("@name", student.name)

cmdgetparentid.parameters.add("@name", parent.name)

dim transaction as sqltransaction

try
cnn.open()
dim studentid, parentid as integer

cmdstudent.executenonquery()
cmdparent.executenonquery()
studentid = cmdgetstudentid.executescalar
parentid = cmdgetparentid.executescalar
cmdstudentparent.parameters.add("@studentid", studentid)
cmdstudentparent.parameters.add("@parentid", parentid)
cmdstudentparent.executenonquery()

contextutil.setcomplete()

catch ex as exception
success = false
contextutil.setabort()
messagebox.show(ex.message)
finally

cnn.close()
end try
return success

end function
注:運用contextutil的靜態方法setcomplete和setabort來提交和回滾。



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 布尔津县| 德令哈市| 光泽县| 彭阳县| 射阳县| 台江县| 错那县| 朝阳县| 玛纳斯县| 泰和县| 温宿县| 息烽县| 鄱阳县| 涿鹿县| 神农架林区| 湟中县| 绥芬河市| 旅游| 英吉沙县| 仙游县| 乳山市| 静乐县| 天水市| 清水县| 富锦市| 昌江| 满洲里市| 邵阳市| 阿克陶县| 和田市| 和林格尔县| 宁河县| 汾阳市| 宜黄县| 阳朔县| 洛扎县| 深水埗区| 乌兰浩特市| 来凤县| 和顺县| 民县|