Supplying parameters at runtime

From InterBase
Jump to: navigation, search

Go Up to Setting parameters


To create parameters at runtime, you can use the:

  • ParamByName method to assign values to a parameter based on its name.
  • Params property to assign values to a parameter based on the parameter’s ordinal position within the SQL statement.
  • Params.ParamValues property to assign values to one or more parameters in a single command line, based on the name of each parameter set. This method uses variants and avoids the need to cast values.
Note: In dialect 3, parameter names passed to functions are case-sensitive.

For all of the examples below, assume the SQL property contains the SQL statement below. All three parameters used are of data type ftString.

<psource lang=sql> INSERT INTO "COUNTRY.DB" (Name, Capital, Continent) VALUES (:Name, :Capital, :Continent) </source>

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

IBQuery1.ParamByName(‘Capital’).AsString := Edit1.Text;

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

IBQuery1.Params[1].AsString := Edit1.Text;

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

IBQuery1.Params.ParamValues[‘Country;Capital;Continent’] :=
  VarArrayOf([Edit1.Text, Edit2.Text, Edit3.Text]);

Advance To: