简体   繁体   English

具有相同连接/事务的多个using语句…-.NET

[英]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.

相关问题 在多个 .NET 应用程序中引用相同的连接字符串 - Reference same Connection String in Multiple .NET Applications 如何使用 SMO for .NET 恢复多个事务日志 - How to restore multiple transaction logs using SMO for .NET TransactionScope,相同连接字符串的事务升级 - TransactionScope, Transaction Escalation for the same connection string 是否可以在.net的同一事务中并行运行多个SQL语句? - Is it possible to run multiple SQL statements in parallel within the same transaction from .net? 在if语句中使用连接字符串 - Using connection string in if statement .Net中的多个连接字符串 - Multiple connection strings in .Net Spring.NET [Transaction]属性是否支持多个事务管理器? - Does Spring.NET [Transaction] attribute support multiple transaction managers? Python for .NET:在多个版本中使用相同的.NET程序集 - Python for .NET: Using same .NET assembly in multiple versions 使用 dapper 在 asp.net core 中的多个连接字符串 - Multiple connection strings in asp.net core using dapper 使用 .NET Core 中的一个连接通过 FTP 下载多个文件 - Download multiple files over FTP using one connection in .NET Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM