Send feedback on this topic.
Teradata.Client.Provider
Prepare Method
Example 



Teradata.Client.Provider Namespace > TdCommand Class : Prepare Method
Prepares the SQL statement (CommandText Property).
Syntax
'Declaration
 
Public Overrides NotOverridable Sub Prepare() 
'Usage
 
Dim instance As TdCommand
 
instance.Prepare()
public override void Prepare()
public:
void Prepare(); override 
Exceptions
ExceptionDescription
Indicates the TdConnection is not in Open state or is null.
Indicates the TdCommand is closed / disposed.
Can be thrown because of one of the following errors: 1) the Advanced SQL Engine returned an error. 2) The Data Provider detected an error.
Remarks

Prepare method sends the SQL statement to the SQL Engine which parses the statement and returns column information.

When TdCommand.CommandType is set to CommandType.StoredProcedure, TdCommand generates a CALL statement and utilizes the parameter collection to create the stored procedure arguments.

If you call one of the Execute methods after calling Prepare, the Data Provider will utilize the Statement Cache. However, if you call one of the Execute methods without calling Prepare, the .Net Data Provider will not utilize the Statement Cache.

If you change either the TdCommand.CommandText or TdCommand.CommandType property, the TdCommand is no longer in a Prepare state.

We strongly recommend that you Prepare parameterized queries that are executed frequently. That is first set the TdCommand.CommandText property, TdCommand.CommandType property and the Parameter collections. Next, prepare the command once and set parameter values and call execute method repeatedly.

Example
The following example creates a TdConnection object and a TdCommand object. It prepares the command and executes the command repeatedly.
public void ExecuteSingleParameterQuery(
                                        string connectionString, 
                                        string commandText, 
                                        String[] parameterArray )
{
    // Open a session to Teradata.
    TdConnection cn = new TdConnection(connectionString);
            
    // Initialize a command to use the session.
    TdCommand cmd = new TdCommand(commandText, cn);
            
    try
    {
        cn.Open();
            
        // Setup parameter information
        TdParameter param = cmd.CreateParameter();
        param.DbType = DbType.String;
        param.Direction = ParameterDirection.Input;
        cmd.Parameters.Add(param);
            
        // Prepare the SQL statement 				
        cmd.Prepare();
            
        foreach (String value in parameterArray)
        {
            // Initialize the parameter value
            param.Value = value;
            
            // Execute the command.
            cmd.ExecuteNonQuery();
        }
    }
    finally
    {
        cmd.Dispose();
        cn.Close();
    }
}
Requirements

Target Platforms: Windows 8.1, Windows 10, Windows Server 2012 R2, Windows Server 2016, Windows Server 2019

See Also

Reference

TdCommand Class
TdCommand Members