[英]Multiple using statement with same connection/transaction… - .NET
I want to do 2 methods, one to do the insert of only one register, and other to do the insert os many registers (a list). 我想做2种方法,一种是只插入一个寄存器,另一种是插入很多寄存器(一个列表)。
But i want the second method to call the first method, just to use the same INSERT code. 但是我想第二种方法调用第一种方法,只是使用相同的INSERT代码。
Is there a way to use the connection opened in the second method, in the first method, so i can call a rollback for all? 有没有一种方法可以使用在第二种方法(在第一种方法中)打开的连接,因此我可以为所有人调用回滚?
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
Thanks 谢谢
Does something like this work - the single and multiple methods both create their connections and pass the connection to a 3rd method that does the work: 做这样的事情-单个和多个方法都创建它们的连接,并将该连接传递给执行该工作的第三个方法:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.