IDbCommand 介面


代表在連接至資料來源時執行的 SQL 陳述式,而且是由存取關聯式資料庫的 .NET Framework 資料提供者所實作。Represents an SQL statement that is executed while connected to a data source, and is implemented by .NET Framework data providers that access relational databases.

public interface class IDbCommand : IDisposable
public interface IDbCommand : IDisposable
type IDbCommand = interface
    interface IDisposable
Public Interface IDbCommand
Implements IDisposable


下列範例會建立衍生類別的實例,SqlConnectionSqlCommandSqlDataReaderThe following example creates instances of the derived classes, SqlConnection, SqlCommand, and SqlDataReader. 此範例會讀取資料,並將其寫入主控台。The example reads through the data, writing it to the console. 最後,此範例會關閉 SqlDataReader,然後 SqlConnectionFinally, the example closes the SqlDataReader, then the SqlConnection.

private static void ReadOrderData(string connectionString)
    string queryString = 
        "SELECT OrderID, CustomerID FROM dbo.Orders;";
    using (SqlConnection connection = new SqlConnection(
        SqlCommand command = new SqlCommand(
            queryString, connection);
        using(SqlDataReader reader = command.ExecuteReader())
            while (reader.Read())
                Console.WriteLine(String.Format("{0}, {1}",
                    reader[0], reader[1]));
Public Sub ReadOrderData(ByVal connectionString As String)
    Dim queryString As String = _
        "SELECT OrderID, CustomerID FROM dbo.Orders;"
    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(queryString, connection)
        Dim reader As SqlDataReader = command.ExecuteReader()
            While reader.Read()
                Console.WriteLine(String.Format("{0}, {1}", _
                    reader(0), reader(1)))
            End While
            ' Always call Close when done reading.
        End Try
    End Using
End Sub


IDbCommand 介面可讓繼承類別實作為命令類別,以代表在資料來源執行的 SQL 語句。The IDbCommand interface enables an inheriting class to implement a Command class, which represents an SQL statement that is executed at a data source. 如需命令類別的詳細資訊,請參閱執行命令For more information about Command classes, see Executing a Command.

應用程式不會直接建立 IDbCommand 介面的實例,而是會建立可執行 IDbCommand 介面之類別的實例。An application does not create an instance of the IDbCommand interface directly, but creates an instance of a class that implements the IDbCommand interface.

IDbCommand 的類別必須實作為其所有成員,而且通常會定義其他成員來新增提供者特有的功能。Classes that implement IDbCommand must implement all its members, and typically define additional members to add provider-specific functionality. 例如,IDbCommand 介面會定義 ExecuteNonQuery 方法。For example, the IDbCommand interface defines the ExecuteNonQuery method. 接著,SqlCommand 類別會繼承這個方法,而且也會定義 ExecuteXmlReader 方法。In turn, the SqlCommand class inherits this method, and also defines the ExecuteXmlReader method.


若要提升 .NET Framework 資料提供者之間的一致性,請以 PrvClassname 的形式命名繼承類別,其中 Prv 是指定給特定 .NET Framework Data Provider 命名空間中所有類別的統一前置詞。To promote consistency among .NET Framework data providers, name the inheriting class in the form PrvClassname where Prv is the uniform prefix given to all classes in a specific .NET Framework data provider namespace. 例如,SqlSystem.Data.SqlClient 命名空間中 SqlCommand 類別的前置詞。For example, Sql is the prefix of the SqlCommand class in the System.Data.SqlClient namespace.

當您從 IDbCommand 介面繼承時,您應該執行下列的函式:When you inherit from the IDbCommand interface, you should implement the following constructors:

項目Item 說明Description
PrvCommand()PrvCommand() 初始化 PrvCommand 類別的新實例。Initializes a new instance of the PrvCommand class.
PrvCommand (字串 cmdText)PrvCommand(string cmdText) 使用查詢的文字,初始化 PrvCommand 類別的新實例。Initializes a new instance of the PrvCommand class with the text of the query.
PrvCommand (string cmdText,PrvConnection connection)PrvCommand(string cmdText, PrvConnection connection) 使用查詢的文字和 PrvConnection,初始化 PrvCommand 類別的新實例。Initializes a new instance of the PrvCommand class with the text of the query and a PrvConnection.
PrvCommand (string cmdText,PrvConnection connection,PrvTransaction transaction)PrvCommand(string cmdText, PrvConnection connection, PrvTransaction transaction) 使用查詢的文字、PrvConnection 和 PrvTransaction,初始化 PrvCommand 類別的新實例。Initializes a new instance of the PrvCommand class with the text of the query, a PrvConnection, and the PrvTransaction.



取得或設定要對資料來源執行的文字命令。Gets or sets the text command to run against the data source.


取得或設定結束執行命令的嘗試並產生錯誤之前的等待時間 (以秒為單位)。Gets or sets the wait time (in seconds) before terminating the attempt to execute a command and generating an error.


指示或指定 CommandText 屬性應如何解譯。Indicates or specifies how the CommandText property is interpreted.


取得或設定 IDbCommand 的這個執行個體所使用的 IDbConnectionGets or sets the IDbConnection used by this instance of the IDbCommand.


取得 IDataParameterCollectionGets the IDataParameterCollection.


取得或設定 .NET Framework 資料提供者的 Command 物件會在其中執行的交易。Gets or sets the transaction within which the Command object of a .NET Framework data provider executes.


取得或設定由 DataRowUpdate(DataSet) 方法使用命令結果時,如何將其套用至 DbDataAdapterGets or sets how command results are applied to the DataRow when used by the Update(DataSet) method of a DbDataAdapter.



嘗試取消 IDbCommand 的執行。Attempts to cancels the execution of an IDbCommand.


建立 IDbDataParameter 物件的新執行個體。Creates a new instance of an IDbDataParameter object.


執行與釋放 (Free)、釋放 (Release) 或重設 Unmanaged 資源相關聯之應用程式定義的工作。Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

(繼承來源 IDisposable)

針對 .NET Framework 資料提供者的 Connection 物件執行 SQL 陳述式,並傳回受影響的資料列數目。Executes an SQL statement against the Connection object of a .NET Framework data provider, and returns the number of rows affected.


針對 Connection 執行 CommandText,並建置 IDataReaderExecutes the CommandText against the Connection and builds an IDataReader.


針對 Connection 執行 CommandText,並使用其中一個 CommandBehavior 值來建置 IDataReaderExecutes the CommandText against the Connection, and builds an IDataReader using one of the CommandBehavior values.


執行查詢,並傳回查詢所傳回的結果集中第一個資料列的第一個資料行。Executes the query, and returns the first column of the first row in the resultset returned by the query. 額外的資料行或資料列都會遭到忽略。Extra columns or rows are ignored.


在資料來源上建立命令已備妥 (或已編譯) 的版本。Creates a prepared (or compiled) version of the command on the data source.