DB2 10.5 for Linux, UNIX, and Windows

DB2Command.Prepare Method

Creates a prepared (or compiled) version of the command at the database.

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

Syntax

[Visual Basic]
NotOverridable Public Sub Prepare()
[C#]
public void Prepare();
[C++]
public: __sealed void Prepare();
[JScript]
public function Prepare();

Exceptions

Exception type Condition
InvalidOperationException The Connection is not set.

-or-

The Connection is not DB2®Connection.Open.

Remarks

Before you call Prepare, specify the data type of each parameter in the statement to be prepared. For each parameter that has a variable length data type, you must set the "DB2Parameter.Size property to the maximum size needed. Prepare returns an error if these conditions are not met.

If you call an Execute method after calling Prepare, any parameter value that is larger than the value specified by the Size property is automatically truncated to the original specified size of the parameter, and no truncation errors are returned.

Output parameters (whether prepared or not) must have a user-specified data type. If you specify a variable length data type, you must also specify the maximum Size.

Example

[Visual Basic, C#] The following example creates a database connection by passing a string to connect to the database, creates a DB2Command by passing in a string that is an SQL SELECT statement and a connection object and opens the connection. The example then prepares the command (the SQL SELECT statement passed in previously) on the database.

[Visual Basic]
Public Sub CreateMyDB2Command(mySelectQuery As String, _
myConnectionString As String)
    Dim myConnection As New DB2Connection(myConnectionString)
    Dim myCommand As New DB2Command(mySelectQuery, myConnection)
    myCommand.Connection.Open()
    myCommand.Prepare()
End Sub

[C#]
public void CreateMyDB2Command(string mySelectQuery, string myConnectionString)
{
   DB2Connection myConnection = new DB2Connection(myConnectionString);
   DB2Command myCommand = new DB2Command(mySelectQuery, myConnection);
   myCommand.Connection.Open();
   myCommand.Prepare();
}