This documentation is archived and is not being maintained.

OleDbCommand.CommandText Property

Gets or sets the SQL statement or stored procedure to execute at 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);

Property Value

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

Implements

IDbCommand.CommandText

Remarks

When the CommandType property is set to StoredProcedure, the CommandText property should be set to the name of the stored procedure. The user may be required to use escape character syntax if the stored procedure name contains any special characters. The command executes this stored procedure when you call one of the Execute methods.

When CommandType is set to TableDirect, the CommandText property should be set to the name of the table or tables to be accessed. The user may be required to use escape character syntax if any of the tables named contain any special characters. All rows and columns of the named table or tables will be returned when you call one of the Execute methods.

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

The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL Statement or a stored procedure called by an OleDbCommand 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 OleDbParameter objects are added to the OleDbParameterCollection must directly correspond to the position of the question mark placeholder for the parameter.

For more information, see Using Stored Procedures with a Command.

Example

[Visual Basic, C#, C++] The following example creates an OleDbCommand and sets some of its properties.

[Visual Basic] 
Public Sub CreateMyOleDbCommand()
    Dim myCommand As New OleDbCommand()
    myCommand.CommandText = "SELECT * FROM Categories ORDER BY CategoryID"
    myCommand.CommandTimeout = 20
End Sub

[C#] 
public void CreateMyOleDbCommand() 
{
   OleDbCommand myCommand = new OleDbCommand();
   myCommand.CommandText = "SELECT * FROM Categories ORDER BY CategoryID";
   myCommand.CommandTimeout = 20;
}

[C++] 
public:
 void CreateMyOleDbCommand() 
 {
    OleDbCommand* myCommand = new OleDbCommand();
    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 Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

OleDbCommand Class | OleDbCommand Members | System.Data.OleDb Namespace | Connection | CommandTimeout | CommandType

Show: