ExecuteReader Method (CommandBehavior)
Collapse the table of content
Expand the table of content

OleDbCommand.ExecuteReader Method (CommandBehavior)

.NET Framework 1.1

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

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

Parameters

behavior
One of the CommandBehavior values.

Return Value

An OleDbDataReader object.

Exceptions

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

Remarks

When you specify SingleRow with the ExecuteReader method of the OleDbCommand object, the .NET Framework Data Provider for OLE DB performs binding using the OLE DB IRow interface if it is available. Otherwise, it uses the IRowset interface. If your SQL statement is expected to return only a single row, specifying SingleRow can also improve application performance.

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.

The OleDbDataReader supports a special mode that enables large binary values to be read efficiently. For more information, see the SequentialAccess setting for CommandBehavior.

While the OleDbDataReader is in use, the associated OleDbConnection is busy serving the OleDbDataReader. While in this state, no other operations can be performed on the OleDbConnection other than closing it. This is the case until you call the OleDbDataReader.Close method. If the OleDbDataReader is created with CommandBehavior set to CloseConnection, closing the OleDbDataReader closes the connection automatically.

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

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

OleDbCommand Class | OleDbCommand Members | System.Data.OleDb Namespace | OleDbCommand.ExecuteReader Overload List

Show:
© 2016 Microsoft