• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

.net 中的事务总结

发布: 2007-7-01 11:09 | 作者: admin | 来源: | 查看: 26次 | 进入软件测试论坛讨论

领测软件测试网 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来提交和回滚。



文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网