This documentation is archived and is not being maintained.

OleDbCommand.ExecuteReader Method

Sends the CommandText to the Connection and builds an OleDbDataReader.

Overload List

Sends the CommandText to the Connection and builds an OleDbDataReader.

[Visual Basic] Overloads Public Function ExecuteReader() As OleDbDataReader
[C#] public OleDbDataReader ExecuteReader();
[C++] public: OleDbDataReader* ExecuteReader();
[JScript] public function ExecuteReader() : OleDbDataReader;

Sends the CommandText to the Connection, and builds an OleDbDataReader using one of the CommandBehavior values.

[Visual Basic] Overloads Public Function ExecuteReader(CommandBehavior) As OleDbDataReader
[C#] public OleDbDataReader ExecuteReader(CommandBehavior);
[C++] public: OleDbDataReader* ExecuteReader(CommandBehavior);
[JScript] public function ExecuteReader(CommandBehavior) : OleDbDataReader;

Example

[Visual Basic, C#, C++] The following example creates an OleDbCommand, then executes it by passing a string that is a Transact-SQL SELECT statement, and a string to use to connect to the data source. CommandBehavior is set to CloseConnection.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of ExecuteReader. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Public Sub CreateMyOleDbDataReader(mySelectQuery As String, _
myConnectionString As String)
    Dim myConnection As New OleDbConnection(myConnectionString)
    Dim myCommand As New OleDbCommand(mySelectQuery, myConnection)
    myConnection.Open()
    Dim myReader As OleDbDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    While myReader.Read()
        Console.WriteLine(myReader.GetString(0))
    End While
    myReader.Close()
    'Implicitly closes the connection because CommandBehavior.CloseConnection was specified.
 End Sub


[C#] 
public void CreateMyOleDbDataReader(string mySelectQuery,string myConnectionString) 
{
   OleDbConnection myConnection = new OleDbConnection(myConnectionString);
   OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
   myConnection.Open();
   OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
   while(myReader.Read()) 
   {
      Console.WriteLine(myReader.GetString(0));
   }
   myReader.Close();
   //Implicitly closes the connection because CommandBehavior.CloseConnection was specified.
}


[C++] 
public:
    void CreateMyOleDbDataReader(String* mySelectQuery, String* myConnectionString)
    {
        OleDbConnection* myConnection = new OleDbConnection(myConnectionString);
        OleDbCommand* myCommand = new OleDbCommand(mySelectQuery, myConnection);
        myConnection->Open();
        OleDbDataReader* myReader = myCommand->ExecuteReader(CommandBehavior::CloseConnection);
        while(myReader->Read())
        {
            Console::WriteLine(myReader->GetString(0));
        }
        myReader->Close();
        //Implicitly closes the connection because CommandBehavior::CloseConnection was specified.
    };

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

OleDbCommand Class | OleDbCommand Members | System.Data.OleDb Namespace

Show: