DB2 Version 10.1 for Linux, UNIX, and Windows

DB2Command.ExecuteReader Method

Sends the CommandText to the Connection and builds a DB2DataReader.

Overload list

Name Description
ExecuteReader() Sends the CommandText to the Connection and builds a DB2®DataReader.
ExecuteReader(CommandBehavior) Sends the CommandText to the Connection, and builds a DB2DataReader based on the CommandBehavior specified.

Example

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

Note: [Visual Basic, C#] 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 CreateMyDB2DataReader(mySelectQuery As String, _
myConnectionString As String)
    Dim myConnection As New DB2Connection(myConnectionString)
    Dim myCommand As New DB2Command(mySelectQuery, myConnection)
    myCommand.Connection.Open()
    Dim myReader As DB2DataReader = 
        myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    While myReader.Read()
        Console.WriteLine(myReader.GetString(0))
    End While
    myReader.Close()
    myConnection.Close()
 End Sub

[C#]
public void CreateMyDB2DataReader(string mySelectQuery,string myConnectionString)
{
   DB2Connection myConnection = new DB2Connection(myConnectionString);
   DB2Command myCommand = new DB2Command(mySelectQuery, myConnection);
   myCommand.Connection.Open();
   DB2DataReader myReader = 
   myCommand.ExecuteReader(CommandBehavior.CloseConnection);
   while(myReader.Read())
   {
      Console.WriteLine(myReader.GetString(0));
   }
   myReader.Close();
   myConnection.Close();
}