Condividi tramite


SqlCommand.CommandText Proprietà

Definizione

Ottiene o imposta l'istruzione Transact-SQL, il nome della tabella o la stored procedure da eseguire nell'origine dati.

public:
 virtual property System::String ^ CommandText { System::String ^ get(); void set(System::String ^ value); };
public:
 property System::String ^ CommandText { System::String ^ get(); void set(System::String ^ value); };
public override string CommandText { get; set; }
[System.Data.DataSysDescription("DbCommand_CommandText")]
public string CommandText { get; set; }
member this.CommandText : string with get, set
[<System.Data.DataSysDescription("DbCommand_CommandText")>]
member this.CommandText : string with get, set
Public Overrides Property CommandText As String
Public Property CommandText As String

Valore della proprietà

Istruzione Transact-SQL o stored procedure da eseguire. Il valore predefinito è una stringa vuota.

Implementazioni

Attributi

Esempio

Nell'esempio seguente viene creato un SqlCommand oggetto e vengono impostate alcune delle relative proprietà.

public void CreateCommand()
 {
    SqlCommand command = new SqlCommand();
    command.CommandText = "SELECT * FROM Categories ORDER BY CategoryID";
    command.CommandTimeout = 15;
    command.CommandType = CommandType.Text;
 }
Public Sub CreateCommand()
    Dim command As New SqlCommand()
    command.CommandText = "SELECT * FROM Categories ORDER BY CategoryID"
    command.CommandTimeout = 15
    command.CommandType = CommandType.Text
End Sub

Commenti

Quando la CommandType proprietà è impostata su StoredProcedure, la CommandText proprietà deve essere impostata sul nome della stored procedure. L'utente può essere necessario utilizzare la sintassi dei caratteri di escape se il nome della stored procedure contiene caratteri speciali. Il comando esegue questa stored procedure quando si chiama uno dei Execute metodi .

Il provider di dati Microsoft .NET Framework per SQL Server non supporta il segnaposto punto interrogativo (?) per passare parametri a un'istruzione Transact-SQL o a una stored procedure chiamata da un comando di CommandType.Text. In questo caso, i parametri denominati devono essere usati. Per esempio:

SELECT * FROM dbo.Customers WHERE CustomerID = @CustomerID

Per altre informazioni, vedere Configuring Parameters and Parameter Data Types.

Si applica a

Vedi anche