Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
ExecuteReader Method

SqlCommand.ExecuteReader Method

Sends the CommandText to the Connection and builds a SqlDataReader.

Overload List

Sends the CommandText to the Connection and builds a SqlDataReader.

Supported by the .NET Compact Framework.

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

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

Supported by the .NET Compact Framework.

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

Example

[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, 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 CreateMySqlDataReader(mySelectQuery As String, _
myConnectionString As String)
    Dim myConnection As New SqlConnection(myConnectionString)
    Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
    myConnection.Open()
    Dim myReader As SqlDataReader = 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 CreateMySqlDataReader(string mySelectQuery,string myConnectionString) 
{
   SqlConnection myConnection = new SqlConnection(myConnectionString);
   SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);
   myConnection.Open();
   SqlDataReader 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 CreateMySqlDataReader(String* mySelectQuery, String* myConnectionString)
    {
        SqlConnection* myConnection = new SqlConnection(myConnectionString);
        SqlCommand* myCommand = new SqlCommand(mySelectQuery, myConnection);
        myConnection->Open();
        SqlDataReader* 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

SqlCommand Class | SqlCommand Members | System.Data.SqlClient Namespace

Show:
© 2015 Microsoft