SqlCommand.CommandText 屬性

定義

取得或設定要在資料來源執行的 Transact-SQL 陳述式、資料表名稱或預存程序。

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

屬性值

要執行的 Transact-SQL 陳述式或預存程序。 預設為空字串。

實作

屬性

範例

下列範例會建立 並 SqlCommand 設定其部分屬性。

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

備註

CommandType當 屬性設定為 StoredProcedure 時, CommandText 屬性應該設定為預存程式的名稱。 如果預存程式名稱包含任何特殊字元,則使用者可能需要使用逸出字元語法。 當您呼叫其中 Execute 一個方法時,此命令會執行這個預存程式。

Microsoft .NET Framework data Provider for SQL Server 不支援問號 (?) 預留位置,將參數傳遞至 Transact-SQL 語句或 命令 CommandType.Text 所呼叫的預存程式。 在此情況下,必須使用具名參數。 例如:

SELECT * FROM dbo.Customers WHERE CustomerID = @CustomerID

如需詳細資訊,請參閱 設定參數和參數資料類型

適用於

另請參閱