This documentation is archived and is not being maintained.

OleDbCommand.CommandType Property

Gets or sets a value indicating how the CommandText property is interpreted.

[Visual Basic]
Public Overridable Property CommandType As CommandType  Implements _
   IDbCommand.CommandType
[C#]
public virtual CommandType CommandType {get; set;}
[C++]
public: __property virtual CommandType get_CommandType();
public: __property virtual void set_CommandType(CommandType);
[JScript]
public function get CommandType() : CommandType;
public function set CommandType(CommandType);

Property Value

One of the CommandType values. The default is Text.

Implements

IDbCommand.CommandType

Exceptions

Exception Type Condition
ArgumentException The value was not a valid CommandType.

Remarks

When you set the CommandType property to StoredProcedure, you should set the CommandText property to the name of the stored procedure. The command executes this stored procedure when you call one of the Execute methods.

The Connection, CommandType and CommandText properties cannot be set 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 instance of a derived class 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.CommandType = CommandType.Text
End Sub

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

[C++] 
public:
 void CreateMyOleDbCommand() 
 {
    OleDbCommand* myCommand = new OleDbCommand();
    myCommand->CommandText = S"SELECT * FROM Categories ORDER BY CategoryID";
    myCommand->CommandType = CommandType::Text;
 }

[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 | CommandText | UpdatedRowSource

Show: