我想做2个方法,一个只插入一个寄存器,另一个插入很多寄存器(列表)。
但我希望第二种方法调用第一种方法,只是使用相同的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
由于
答案 0 :(得分: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