Search code examples
vb.netasynchronousdatareader

Selecting DataReader type based on database version


I am trying to set my DbDataReader dynamically based on the database version (Oracle or SQL) within an async function. The function GetReaderAsync should return the type of DataReader so I can use the generic reader with rest of my operations. That is my purpose, so don't feel obliged to fix this code that might be based on a wrong idea, but do provide sample code to accomplish the requirement. At the moment I'm getting a compilation error ReadAsync is not a member of System.Threading.Tasks.Task(Of System.Data.Common.DbDataReader) I believe I'm doing something wrong, maybe in the GetReaderAsync function.

 Using reader As Task(Of Common.DbDataReader) = ConnectionController.GetReaderAsync(command) ' CType(Await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess), Oracle.DataAccess.Client.OracleDataReader)
                    While Await reader.ReadAsync() '<-- Compilation Error
                        If reader.HasRows Then
                        End If
                   End While
End Using



Public Shared Async Function GetReaderAsync(command As DbCommand) As Threading.Tasks.Task(Of DbDataReader)
        'Depending on the XML value provided in the [Provider], then select the appropriate connection from 
        'the object oSettings of the SettingsController. The SettingsController provides a structure called Provider to choose from
        If SettingsController.oSettings.Connection.provider.ToLower = SettingsController.Provider.Oracle Then
            Using reader As Oracle.DataAccess.Client.OracleDataReader = CType(Await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess), Oracle.DataAccess.Client.OracleDataReader)
                command.ExecuteReader()
                Return reader
            End Using
        Else
            Using reader As SqlClient.SqlDataReader = CType(Await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess), SqlClient.SqlDataReader)
                command.ExecuteReader()
                Return reader
            End Using
        End If
    End Function

Solution

  • You can use the Interface "System.Data.IDataReader". I use it for MySql, MSSql and Sqlite. It works very fine.