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来提交和回滚。