Export (0) Print
Expand All

SqlCommand.CommandText Property

Gets or sets the Transact-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 Transact-SQL statement or stored procedure to execute. The default 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.

The .NET Framework Data Provider for SQL Server does not support the question mark (?) placeholder for passing parameters to a Transact-SQL statement or a stored procedure called by a command of CommandType.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.

Example

[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.CommandText = "SELECT * FROM Categories ORDER BY CategoryID"
    myCommand.CommandTimeout = 15
    myCommand.CommandType = CommandType.Text
End Sub 'CreateMySqlCommand

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

[C++] 
public:
void CreateMySqlCommand() 
 {
    SqlCommand* myCommand = new SqlCommand();
    myCommand->CommandText = S"SELECT * FROM Categories ORDER BY CategoryID";
    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.

Requirements

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 | Connection | CommandTimeout | CommandType

Show:
© 2014 Microsoft