System.Data.Odbc.OdbcCommand.CommandText Property

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

Syntax

[System.ComponentModel.RefreshProperties(System.ComponentModel.RefreshProperties.All)]
[System.ComponentModel.Editor("Microsoft.VSDesigner.Data.Odbc.Design.OdbcCommandTextEditor, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[System.ComponentModel.DefaultValue("")]
public override string CommandText { set; get; }

Value

Documentation for this section has not yet been entered.

Remarks

When the System.Data.IDbCommand.CommandType property is set to StoredProcedure, the OdbcCommand.CommandText property should be set using standard ODBC stored procedure escape sequences. Setting the OdbcCommand.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:

Example

{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

Example

{[?=]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, OdbcCommand.ExecuteReader or OdbcCommand.ExecuteNonQuery).

You cannot set the OdbcCommand.Connection, OdbcCommand.CommandType and OdbcCommand.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 System.Data.Odbc.OdbcCommand when OdbcCommand.CommandType is set to Text. In this case, the question mark (?) placeholder must be used. For example:

Example

SELECT * FROM Customers WHERE CustomerID = ?

Therefore, the order in which System.Data.Odbc.OdbcParameter objects are added to the System.Data.Odbc.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 System.Data.Odbc.OdbcParameterCollection:

Example

{1, null, 2}

passed into the OdbcCommand.CommandText property:

Example

{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 System.Data.Odbc.OdbcParameterCollection:

Example

{1, 2}

and set the OdbcCommand.CommandText property to the following:

Example

{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.

Requirements

Namespace: System.Data.Odbc
Assembly: System.Data (in System.Data.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0