OracleCommand ExecuteNonQuery 与异步

OracleCommand ExecuteNonQuery with Async

Public Function ExecuteNonQuery(ByVal cmd As OracleCommand) As Integer
    ' no of affected rows ...
    Dim affectedRows As Integer = -1
    ' execute the command ...
    Try
        ' open connection ...
        cmd.Connection.Open()
    Catch ex As Exception
        MsgBox("Unable to establish a connection to database." & ex.ToString, MsgBoxStyle.Critical)
        Exit Function
    End Try
    Try
        ' execute command ...
        'affectedRows = cmd.ExecuteNonQuery()
        affectedRows = cmd.ExecuteNonQuery()

    Catch ex As OracleClient.OracleException
        If CInt(ex.Code) = CInt(1) Then
            Return -2
        End If

    Catch ex As Exception
        ' rethrow error ...
        'Dim cfrErr As New CFR_Errors("Data_Access_Class","ExecuteNonQuery", ex)
        Throw New Exception(ex.InnerException.ToString, ex)
    Finally
        cmd.Connection.Close()
    End Try
    Return affectedRows
End Function

如何使用 Oracle.ManagedDataAccess 执行我的异步查询?我们正在将我们的数据库更改为 AWS,它可能会引入延迟,

足够简单,可以重构为使用 AsyncAwait,因为 DbConnection DbCommand 应该有异步 API 可用。

Public Async Function ExecuteNonQueryAsync(ByVal cmd As DbCommand) As Task(Of Integer)
    ' no of affected rows ...
    Dim affectedRows As Integer = -1
    ' execute the command ...
    Try
        ' open connection ...
        Await cmd.Connection.OpenAsync()
    Catch ex As Exception
        MsgBox("Unable to establish a connection to CFR database." & ex.ToString, MsgBoxStyle.Critical)
        Exit Function
    End Try
    Try
        ' execute command ...
        affectedRows = Await cmd.ExecuteNonQueryAsync()

    Catch ex As OracleClient.OracleException
        If CInt(ex.Code) = CInt(1) Then
            Return -2
        End If

    Catch ex As Exception
        ' rethrow error ...
        Throw New Exception(ex.InnerException.ToString, ex)
    Finally
        cmd.Connection.Close()
    End Try
    Return affectedRows
End Function

引用Asynchronous programming with Async and Await (Visual Basic)