dotConnect for DB2 Documentation
In This Topic
    Using Parameters
    In This Topic

    dotConnect for DB2 enhances SQL handling capabilities with usage of parameters in SQL queries. You can make execution of a query very flexible using several simple techniques. This article describes some basics you must be acquainted with when working with parameters in dotConnect for DB2.
    The article consists of following sections:

    Parameters basics

    In general, parameter is a placeholder for a variable that contains some value of some type when executing a general-purpose query. Parameter is represented by DB2Parameter class. All parameters that take part in query execution constitute a collection that can be accessed through DB2Command.Parameters property.

    There are two kinds of parameters exist: named and unnamed.

    Unnamed parameters can be specified as '?' symbol. The following query

        INSERT INTO dept (deptno, dname, loc) VALUES (?, ?, ?)

    declares that three parameters are required to run the query properly. To set parameters for this query you can use the next code:

    DB2Command myCommand1;
    ...
    myCommand1.CommandText = "INSERT INTO dept (deptno, dname, loc) VALUES (?, ?, ?)";
    myCommand1.Parameters.Add("param1", 30);
    myCommand1.Parameters.Add("param2", "SALES");
    myCommand1.Parameters.Add("param3", "CHICAGO");
    
    
    Dim myCommand1 as DB2Command
    ...
    myCommand1.CommandText = "INSERT INTO dept (deptno, dname, loc) VALUES (?, ?, ?)"
    myCommand1.Parameters.Add("param1", 30)
    myCommand1.Parameters.Add("param2", "SALES")
    myCommand1.Parameters.Add("param3", "CHICAGO")
    
    

    Named parameters require accordance with names of DB2Parameter instances in the collection. Named parameters are declared using '@' or ':' prefix followed by name of the parameter. The colon prefix can be used only if HostVarParameters connection string parameter is set to true.

    Note that the name of the DB2Parameter object from the collection of the command should contain the '@' prefix if parameter in CommandText is used with the '@' prefix. If the parameter in CommandText contains the ':' prefix, the name of the DB2Parameter object in the command's collection should be used without any prefix. You can use these prefixes at any combinations to specify parameters. There are two main advantages of named parameters. First, you do not have to care about the order in which parameters are created. Second, named parameter can appear more than once in query text, but you have to create only one instance of it in Parameters collection.

    For example, a simple Update statement that requires named parameters might look like the following:

        UPDATE dept SET dname = @dname, loc = @loc WHERE deptno = @deptno

    To set parameters for this query you can use the next code:

    DB2Command myCommand1;
    ...
    myCommand1.CommandText = "UPDATE dept SET dname = @dname, loc = @loc WHERE deptno = @deptno";
    myCommand1.Parameters.Add("@deptno", 20);
    myCommand1.Parameters.Add("@dname", "SALES");
    myCommand1.Parameters.Add("@loc", "NEW YORK");
    
    
    Dim myCommand1 as DB2Command
    ...
    myCommand1.CommandText = "UPDATE dept SET dname = @dname, loc = @loc WHERE deptno = @deptno"
    myCommand1.Parameters.Add("@deptno", 20)
    myCommand1.Parameters.Add("@dname", "SALES")
    myCommand1.Parameters.Add("@loc", "NEW YORK")
    
    

    Using automatic parameters synchronization

    The behavior described above assumes that DB2Command.ParameterCheck is false (by default). By turning it on you enable automatic synchronization of query text and DB2Command.Parameters collection. In this mode all parameters are checked for validity, new ones are added if necessary, and redundant parameters are deleted. Thus you do not have to take care about quantity of items in DB2Command.Parameters collection, you can specify only the ones you really need. The synchronization is done as follows:

    The synchronization takes place when:

    Performance issues

    In general, setting DB2Command.ParameterCheck property to true leads to some performance loss.
    The synchronization is performed on client, so performance reduces slightly.

    To optimize multiple execution of a query you can manually call DB2Command.Prepare method that creates compiled version of the query on server and forces synchronization. After the query is prepared it is not advised to alter DB2Command.Text or DB2Parameter.Direction properties because the DB2Command becomes unprepared again. The recommended practice is to set up query text and parameters, execute DB2Command.Prepare method, and afterwards change only parameters' values.

    See Also

    DB2Command Class  | DB2Parameter Class