DB2 Version 10.1 for Linux, UNIX, and Windows

DB2Command Class

Represents an SQL statement or stored procedure to execute against a data source.

Namespace:
IBM®.Data.DB2®
Assembly:
IBM.Data.DB2 (in IBM.Data.DB2.dll)

.NET Framework 2.0 3.0, 3.5, and 4.0 Inheritance hierarchy

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Data.Common.DbCommand
            IBM.Data.DB2.DB2Command

.NET Framework 2.0 3.0, 3.5, and 4.0 Syntax

[Visual Basic]
NotInheritable Public Class DB2Command
   Inherits DbCommand
   Implements ICloneable
[C#]
public sealed class DB2Command : DbCommand, ICloneable
[C++]
public __gc __sealed class DB2Command : public DbCommand,
   ICloneable
[JScript]
public class DB2Command extends DbCommand implements ICloneable

Remarks

In DB2 V9.7 Fix Pack 5 and later, the DB2Command class provides ArrayBindCountOff constant to reset the ArrayBindCount property.

The DB2Command class provides the following methods for executing commands against a database:

Item Description
ExecuteReader Executes commands that return rows.
ExecuteResultSet Sends the CommandText to the Connection and builds an DB2ResultSet.
ExecuteNonQuery Executes commands such as SQL INSERT, DELETE, UPDATE, and SET statements.
ExecuteXmlReader Sends the CommandText to the Connection and builds an XMLReader.
ExecuteScalar Retrieves a single value (for example, an aggregate value) from a database.

If execution of the command results in a fatal DB2Exception , the DB2Connection may close. However, the user can reopen the connection and continue.

Example

[Visual Basic, C#] The following example uses the ExecuteReader method of DB2Command, along with DB2DataReader and DB2Connection , to select rows from a table.

[Visual Basic]
Public Sub ReadMyData(myConnString As String)
     Dim mySelectQuery As String = "SELECT SALES, SALES_PERSON FROM SALES"
     Dim myConnection As New DB2Connection(myConnString)
     Dim myCommand As New DB2Command(mySelectQuery, myConnection)
     myConnection.Open()
     Dim myReader As DB2DataReader = myCommand.ExecuteReader()
     Try
         While myReader.Read()
             Console.WriteLine(myReader.GetInt32(0).ToString() + ", " _
                + myReader.GetString(1))
         End While
     Finally
         ' always call Close when done reading.
         myReader.Close()
         ' always call Close when done with connection.
         myConnection.Close()
     End Try
 End Sub
[C#] 
public void ReadMyData(string myConnString)
{
    string mySelectQuery = "SELECT SALES, SALES_PERSON FROM SALES";
    DB2Connection myConnection = new DB2Connection(myConnString);
    DB2Command myCommand = new DB2Command(mySelectQuery,myConnection);
    myConnection.Open();
    DB2DataReader myReader = myCommand.ExecuteReader();
    try
    {
       while (myReader.Read())
       {
          Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
       }
    }
    finally 
    {
       // always call Close when done reading.
       myReader.Close();
       // always call Close when done with connection.
       myConnection.Close();
    }
}

Thread safety

Any public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Any instance members are not guaranteed to be thread safe.

Version information

Last update
This topic was last updated for: IBM DB2 Version 9.5
.NET Framework version
Supported in: 2.0, 3.0, 3.5, and 4.0
IBM Data Server Client
Supported in: 8.1.2 and up