事务的一些总结

本文介绍了三种不同级别的事务处理方法:存储过程级别、数据库级别及页面级别(COM级别)。通过实例展示了如何确保学生及其家长信息的一致性插入,涉及SQL命令、异常处理及提交回滚等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 name=@name ", cnn) 

Dim cmdGetParentid As New SqlCommand("select parentid from parent where name=@name", 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 name=@name ", cnn) 

Dim cmdGetParentid As New SqlCommand("select parentid from parent where name=@name", 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来提交和回滚。
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值