OleDbCommand.ExecuteReader Method ()

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Sends the CommandText to the Connection and builds an OleDbDataReader.

Namespace:   System.Data.OleDb
Assembly:  System.Data (in System.Data.dll)

Public Function ExecuteReader As OleDbDataReader

Exception Condition
InvalidOperationException

Cannot execute a command within a transaction context that differs from the context in which the connection was originally enlisted.

When the CommandType property is set to StoredProcedure, the CommandText property should be set to the name of the stored procedure. The command executes this stored procedure when you call ExecuteReader.

Before you close the OleDbConnection, first close the OleDbDataReader object. You must also close the OleDbDataReader object if you plan to reuse an OleDbCommand object.

The following example creates an OleDbCommand, and then executes it by passing a string that is an SQL SELECT statement, and a string to use to connect to the data source.

Public Sub CreateReader(ByVal connectionString As String, _
    ByVal queryString As String)

    Using connection As New OleDbConnection(connectionString)
        Dim command As New OleDbCommand(queryString, connection)

        connection.Open()

        Dim reader As OleDbDataReader = command.ExecuteReader()
        While reader.Read()
            Console.WriteLine(reader(0).ToString())
        End While
        reader.Close()
    End Using
End Sub

.NET Framework
Available since 1.1
Return to top
Show: