RAD Studio |  |

Supplying Parameters at Runtime
See Also
Collapse All

To create parameters at runtime, you can use the

- ParamByName method to assign values to a parameter based on its name (not available for TADOQuery)
- Params or Parameters property to assign values to a parameter based on the parameter's ordinal position within the SQL statement.
- Params.ParamValues or Parameters.ParamValues property to assign values to one or more parameters in a single command line, based on the name of each parameter set.

The following code uses ParamByName to assign the text of an edit box to the :Capital parameter:
Copy Code

    SQLQuery1.ParamByName('Capital').AsString := Edit1.Text;

Copy Code

    SQLQuery1->ParamByName("Capital")->AsString = Edit1->Text;

The same code can be rewritten using the Params property, using an index of 0 (assuming the :Capital parameter is the first parameter in the SQL statement):

Copy Code

    SQLQuery1.Params[0].AsString := Edit1.Text;

Copy Code

    SQLQuery1->Params->Items[0]->AsString = Edit1->Text;

The command line below sets three parameters at once, using the Params.ParamValues property:

Copy Code

    Query1.Params.ParamValues['Name;Capital;Continent'] :=
      VarArrayOf([Edit1.Text, Edit2.Text, Edit3.Text]);

Copy Code

    Query1->Params->ParamValues["Name;Capital;Continent"] =
      VarArrayOf(OPENARRAY(Variant, (Edit1->Text, Edit2->Text, Edit3->Text)));

Note that ParamValues uses Variants, avoiding the need to cast values.

See Also

Supplying Parameters at Design Time

Establishing Master/detail Relationships Using Parameters

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!