dotConnect for Snowflake enhances SQL handling capabilities through the use of parameters in SQL queries. You can make execution of a query very flexible using several techniques. This article describes some basics you must know when working with parameters in dotConnect for Snowflake, as well as parameters synchronization.
In general, a parameter is a placeholder for a variable that contains a value of a certain type when a query is executed. A parameter is represented by the SnowflakeParameter class. All parameters that take part in query execution constitute a collection that can be accessed through the SnowflakeCommand.Parameters property.
dotConnect for Snowflake supports named and positional parameters (see Positional Parameters). Named parameters are declared in the query text using the ':' prefix followed by the name of the parameter. The name of the SnowflakeParameter object in the command's collection can be specified without the prefix or with the ':' or '@' prefix. Named parameters have two main advantages. First, you do not have to care about the order in which parameters are created. Second, a named parameter can appear more than once in the query text, but you have to create only one instance of it in the Parameters collection.
For example, a simple UPDATE statement with named parameters might look like the following:
To set parameters for this query, you can use the following code:
You can also use the positional parameter markers of Snowflake: :1, :2, and so on, or ?. Name the parameters for them 1, 2, and so on. The ? markers are numbered in the order they appear in the query text, and they are bound to the parameters by these names, not by the order of the parameters in the collection:
cmd.CommandText = "UPDATE dept SET loc = ? WHERE deptno = ?";
cmd.Parameters.Add("1", DbType.String).Value = "BOSTON";
cmd.Parameters.Add("2", DbType.Int64).Value = 20; |
If the query text contains a parameter that is not in the collection, the statement fails with an error about a missing bind variable. Parameters of the collection that the query text doesn't use are ignored. Parameter markers inside string literals and comments are treated as text.
The behavior described above assumes that SnowflakeCommand.ParameterCheck is false (by default). If you set it to true, you enable automatic synchronization of the query text and the SnowflakeCommand.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 care about the number of items in the SnowflakeCommand.Parameters collection. The synchronization is performed as follows:
The synchronization takes place when:
The synchronization requires an open connection. If the connection of the command is closed, setting ParameterCheck to true or changing CommandText while ParameterCheck is true throws InvalidOperationException. The SnowflakeCommandBuilder.DeriveParameters method requires an open connection too.