0

我想做两种方法,一种是只插入一个寄存器,另一种是插入多个寄存器(一个列表)。

但我希望第二种方法调用第一种方法,只是为了使用相同的 INSERT 代码。

有没有办法使用在第二种方法中打开的连接,在第一种方法中,所以我可以为所有人调用回滚?

Shared Sub Gravar(ByRef Pessoa As Pessoa)
    Try
        Using con As New myConnection
            con.Open()
            If Pessoa.Id = -1 Then
                Insert(Pessoa, con)
            Else
                Update(Pessoa, con)
            End If
        End Using
    Catch ex As Exception
        Throw
    End Try
End Sub

Shared Sub Gravar(ByRef Pessoas As List(Of Pessoa))
    Try
        Using con As New myConnection
            con.Open()
            con.OpenTransaction()
            For Each Pessoa As Pessoa In Pessoas
                Gravar(Pessoa)
            Next
            con.Commit()
        End Using
    Catch ex As Exception
        Throw
    End Try
End Sub

谢谢

4

1 回答 1

0

做这样的事情 - 单个和多个方法都创建它们的连接并将连接传递给完成工作的第 3 个方法:

Shared Sub Gravar(ByRef Pessoa As Pessoa)
    Try
        Using con As New myConnection
            con.Open()
            _Gravar(Pessoa, con)
        End Using
    Catch ex As Exception
        Throw
    End Try
End Sub

Shared Sub Gravar(ByRef Pessoas As List(Of Pessoa))
    Try
        Using con As New myConnection
            con.Open()
            con.OpenTransaction()
            For Each Pessoa As Pessoa In Pessoas
                _Gravar(Pessoa, con)
            Next
            con.Commit()
        End Using
    Catch ex As Exception
        Throw
    End Try
End Sub

Private Shared Sub _Gravar(ByRef Pessoa As Pessoa, ByVal con As myConnection)
    Try
        If Pessoa.Id = -1 Then
            Insert(Pessoa, con)
        Else
            Update(Pessoa, con)
        End If
    Catch ex As Exception
        Throw
    End Try
End Sub
于 2013-07-04T18:37:31.793 回答