.net 中的事务总结

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

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

dgrwang

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值