This documentation is archived and is not being maintained.

SqlCommand.CommandType Property

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

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

Property Value

One of the CommandType values. The default is Text.




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


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 .NET Framework Data Provider for SQL Server does not support the question mark (?) placeholder for passing parameters to a SQL Statement or a stored procedure called with a CommandType of Text. In this case, named parameters must be used. For example:

SELECT * FROM Customers WHERE CustomerID = @CustomerID

For more information see Using Stored Procedures with a Command.


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

[Visual Basic] 
Public Sub CreateMySqlCommand()
    Dim myCommand As New SqlCommand()
    myCommand.CommandTimeout = 15
    myCommand.CommandType = CommandType.Text
End Sub

public void CreateMySqlCommand() 
   SqlCommand myCommand = new SqlCommand();
   myCommand.CommandTimeout = 15;
   myCommand.CommandType = CommandType.Text;

 void CreateMySqlCommand() 
    SqlCommand* myCommand = new SqlCommand();
    myCommand->CommandTimeout = 15;
    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.


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

See Also

SqlCommand Class | SqlCommand Members | System.Data.SqlClient Namespace | CommandText | CommandTimeout | Connection