This documentation is archived and is not being maintained.

SqlCommand.ExecuteReader Method (CommandBehavior)

.NET Framework 1.1

Sends the CommandText to the Connection, and builds a SqlDataReader using one of the CommandBehavior values.

[Visual Basic]
Overloads Public Function ExecuteReader( _
   ByVal behavior As CommandBehavior _
) As SqlDataReader
public SqlDataReader ExecuteReader(
 CommandBehavior behavior
public: SqlDataReader* ExecuteReader(
 CommandBehavior behavior
public function ExecuteReader(
   behavior : CommandBehavior
) : SqlDataReader;


One of the CommandBehavior values.

Return Value

A SqlDataReader object.


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 SqlDataReader supports a special mode that enables large binary values to be read efficiently. For more information, see the SequentialAccess setting for CommandBehavior.

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


[Visual Basic, C#, C++] The following example creates a SqlCommand, 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 CreateMySqlDataReader(mySelectQuery As String, _
myConnectionString As String)
    Dim myConnection As New SqlConnection(myConnectionString)
    Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
    Dim myReader As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    While myReader.Read()
    End While
    'Implicitly closes the connection because CommandBehavior.CloseConnection was specified.
 End Sub

public void CreateMySqlDataReader(string mySelectQuery,string myConnectionString) 
   SqlConnection myConnection = new SqlConnection(myConnectionString);
   SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);
   SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
   //Implicitly closes the connection because CommandBehavior.CloseConnection was specified.

    void CreateMySqlDataReader(String* mySelectQuery, String* myConnectionString)
        SqlConnection* myConnection = new SqlConnection(myConnectionString);
        SqlCommand* myCommand = new SqlCommand(mySelectQuery, myConnection);
        SqlDataReader* myReader = myCommand->ExecuteReader(CommandBehavior::CloseConnection);
        //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.


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

See Also

SqlCommand Class | SqlCommand Members | System.Data.SqlClient Namespace | SqlCommand.ExecuteReader Overload List