Export (0) Print
Expand All

OdbcCommand.CommandText Property

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

Namespace:  System.Data.Odbc
Assembly:  System.Data (in System.Data.dll)

'Declaration
Public Overrides Property CommandText As String

Property Value

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

Implements

IDbCommand.CommandText

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, ?)}
NoteNote

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.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Reference

[!:System.Data.Odbc.OdbcCommand.CommandTimeout]

Other Resources

Show:
© 2014 Microsoft