IDbCommand IDbCommand IDbCommand IDbCommand Interface

定義

代表在連接至資料來源時執行的 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
衍生
實作

範例

下列範例會建立衍生的類別的執行個體SqlConnectionSqlCommand,和SqlDataReaderThe 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(
               connectionString))
    {
        SqlCommand command = new SqlCommand(
            queryString, connection);
        connection.Open();
        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)
        connection.Open()
        Dim reader As SqlDataReader = command.ExecuteReader()
        Try
            While reader.Read()
                Console.WriteLine(String.Format("{0}, {1}", _
                    reader(0), reader(1)))
            End While
        Finally
            ' Always call Close when done reading.
            reader.Close()
        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 資料提供者命名空間中的所有類別。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. 例如,Sql是的前置詞SqlCommand類別中System.Data.SqlClient命名空間。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 (字串 cmdText、 PrvConnection 連接)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 字串 cmdText、 PrvConnection 連接 (PrvTransaction 交易)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.

屬性

CommandText CommandText CommandText CommandText

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

CommandTimeout CommandTimeout CommandTimeout CommandTimeout

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

CommandType CommandType CommandType CommandType

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

Connection Connection Connection Connection

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

Parameters Parameters Parameters Parameters

取得 IDataParameterCollectionGets the IDataParameterCollection.

Transaction Transaction Transaction Transaction

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

UpdatedRowSource UpdatedRowSource UpdatedRowSource UpdatedRowSource

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

方法

Cancel() Cancel() Cancel() Cancel()

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

CreateParameter() CreateParameter() CreateParameter() CreateParameter()

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

Dispose() Dispose() Dispose() Dispose()

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

(Inherited from IDisposable)
ExecuteNonQuery() ExecuteNonQuery() ExecuteNonQuery() ExecuteNonQuery()

針對 .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.

ExecuteReader() ExecuteReader() ExecuteReader() ExecuteReader()

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

ExecuteReader(CommandBehavior) ExecuteReader(CommandBehavior) ExecuteReader(CommandBehavior) ExecuteReader(CommandBehavior)

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

ExecuteScalar() ExecuteScalar() ExecuteScalar() ExecuteScalar()

執行查詢,並傳回查詢所傳回的結果集中第一個資料列的第一個資料行。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.

Prepare() Prepare() Prepare() Prepare()

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

適用於

另請參閱