·您的位置: 首页 » 资源教程 » 编程开发 » ASP.NET » .net 中的事务总结

.net 中的事务总结

类别: ASP.NET教程  评论数:0 总得分:0
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来提交和回滚。






-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1