OdbcCommand.CommandText Property

Definition

Gets or sets the SQL statement or stored procedure to execute against the data source.

public:
 virtual property System::String ^ CommandText { System::String ^ get(); void set(System::String ^ value); };
public:
 property System::String ^ CommandText { System::String ^ get(); void set(System::String ^ value); };
public override string CommandText { get; set; }
public string CommandText { get; set; }
member this.CommandText : string with get, set
Public Overrides Property CommandText As String
Public Property CommandText As String

Property Value

The SQL statement or stored procedure to execute. The default value is an empty string ("").

Implements

Remarks

When the CommandType property is set to StoredProcedure, the CommandText property should be set using standard ODBC stored procedure escape sequences. Setting the CommandText to the name of the stored procedure does not function as it does for other .NET Framework data providers.

Many language features, such as outer joins and scalar function calls, are generally implemented by data sources. Even the syntax for these features is generally data source-specific. Therefore, ODBC defines escape sequences that contain standard syntax for the following language features:

  • Date, time, timestamp, and datetime interval literals

  • Scalar functions such as numeric, string, and data type conversion functions

  • LIKE predicate escape character

  • Outer joins

  • Procedure calls

The escape sequence used by ODBC is as follows:

{extension}  

This escape sequence is recognized and parsed by ODBC drivers. They then replace any escape sequences with data source-specific grammar.

A procedure is an executable object stored at the data source. Generally, it is one or more SQL statements that have been precompiled. The escape sequence for calling a procedure is

{[?=]call procedure-name[([parameter][,[parameter]]...)]}  

where procedure-name specifies the name of a procedure and parameter specifies a procedure parameter.

The command executes this stored procedure when you call one of the Execute methods (for example, ExecuteReader or ExecuteNonQuery).

You cannot set the Connection, CommandType and CommandText properties if the current connection is performing an execute or fetch operation.

The ODBC.NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OdbcCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used. For example:

SELECT * FROM Customers WHERE CustomerID = ?  

Therefore, the order in which OdbcParameter objects are added to the OdbcParameterCollection must directly correspond to the position of the question mark placeholder for the parameter.

If a parameter contains a null value, the .NET Framework Data Provider for ODBC still binds that parameter, but uses a default parameter, if one has been defined by using SQL_DEFAULT_PARAM, instead of the null value. For example, the OdbcParameterCollection:

{1, null, 2}  

passed into the CommandText property:

{call sp(?, ?, ?)}  

causes the .NET Framework Data Provider for ODBC binding the first parameter to the value 1, the third parameter to the value 2, and the second parameter as SQL_DEFAULT_PARAM. However, this behavior is driver-dependent. If the driver does not support this functionality, just do not pass in a value for the parameter. For example, use the OdbcParameterCollection:

{1, 2}  

and set the CommandText property to the following:

{call sp(?, null, ?)}  

Note

If a parameter is omitted, the comma delimiting it from other parameters must still appear. If an input or input/output parameter is omitted, the procedure uses the default value of the parameter. Another way to specify the default value of an input or input/output parameter is to set the value of the length/indicator buffer bound to the parameter to SQL_DEFAULT_PARAM.

Applies to

See also