Gets or sets the SQL statement or stored procedure to execute against the data source.
[Visual Basic] Public Overridable Property CommandText As String Implements _ IDbCommand.CommandText [C#] public virtual string CommandText {get; set;} [C++] public: __property virtual String* get_CommandText(); public: __property virtual void set_CommandText(String*); [JScript] public function get CommandText() : String; public function set CommandText(String);
The SQL statement or stored procedure to execute. The default value is an empty string ("").
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.
A number of language features, such as outer joins and scalar function calls, are commonly implemented by data sources. Even the syntax for these features tends to be data source-specific. Because of this, ODBC defines escape sequences that contain standard syntax for the following language features:
The escape sequence used by ODBC is as follows:
{extension}
This escape sequence is recognized and parsed by ODBC drivers, which 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 = ?
As a result, 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 using SQL_DEFAULT_PARAM, in place of the null value. (For example, the OdbcParameterCollection:
{1, null, 2}
passed into the CommandText property:
{ call sp(?, ?, ?) }
results in 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. This behavior is driver-dependent, however. If the driver does not support this functionality, simply do not pass in a value for the parameter. For example, use the OdbcParameterCollection:
{1, 2}
and set the CommandText property to:
{ 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.
[Visual Basic, C#, C++] The following example creates an OdbcCommand and sets some of its properties.
[Visual Basic] Public Sub CreateMyOdbcCommand() Dim myCommand As New OdbcCommand() myCommand.CommandText = "SELECT * FROM Categories ORDER BY CategoryID" myCommand.CommandTimeout = 20 End Sub [C#] public void CreateMyOdbcCommand() { OdbcCommand myCommand = new OdbcCommand(); myCommand.CommandText = "SELECT * FROM Categories ORDER BY CategoryID"; myCommand.CommandTimeout = 20; } [C++] public: void CreateMyOdbcCommand() { OdbcCommand* myCommand = new OdbcCommand(); myCommand->CommandText = S"SELECT * FROM Categories ORDER BY CategoryID"; myCommand->CommandTimeout = 20; };
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
OdbcCommand Class | OdbcCommand Members | System.Data.Odbc Namespace | Connection | CommandTimeout | CommandType