OleDbCommand 類別

定義

表示要對資料來源執行的 SQL 陳述式或預存程序。Represents an SQL statement or stored procedure to execute against a data source.

public ref class OleDbCommand sealed : System::Data::Common::DbCommand, ICloneable, IDisposable
public sealed class OleDbCommand : System.Data.Common.DbCommand, ICloneable, IDisposable
type OleDbCommand = class
    inherit DbCommand
    interface ICloneable
    interface IDbCommand
    interface IDisposable
Public NotInheritable Class OleDbCommand
Inherits DbCommand
Implements ICloneable, IDisposable
繼承
實作

範例

下列範例會使用 OleDbCommand,以及 OleDbDataAdapterOleDbConnection,從 Access 資料庫中選取資料列。The following example uses the OleDbCommand, along OleDbDataAdapter and OleDbConnection, to select rows from an Access database. 然後會傳回填滿的 DataSetThe filled DataSet is then returned. 此範例會傳遞初始化的 DataSet、連接字串、屬於 SQL SELECT 語句的查詢字串,以及源資料庫資料表名稱的字串。The example is passed an initialized DataSet, a connection string, a query string that is an SQL SELECT statement, and a string that is the name of the source database table.

public void ReadMyData(string connectionString)
{
    string queryString = "SELECT OrderID, CustomerID FROM Orders";
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        OleDbCommand command = new OleDbCommand(queryString, connection);
        connection.Open();
        OleDbDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            Console.WriteLine(reader.GetInt32(0) + ", " + reader.GetString(1));
        }
        // always call Close when done reading.
        reader.Close();
    }
}
Public Sub ReadMyData(ByVal connectionString As String)
    Dim queryString As String = "SELECT OrderID, CustomerID FROM Orders"
    Using connection As New OleDbConnection(connectionString)
        Dim command As New OleDbCommand(queryString, connection)

        connection.Open()

        Dim reader As OleDbDataReader = command.ExecuteReader()
        While reader.Read()
            Console.WriteLine(reader.GetInt32(0).ToString() + ", " _
               + reader.GetString(1))
        End While

        ' always call Close when done reading.
        reader.Close()
    End Using
End Sub

備註

建立 OleDbCommand 的實例時,讀取/寫入屬性會設定為其初始值。When an instance of OleDbCommand is created, the read/write properties are set to their initial values. 如需這些值的清單,請參閱 OleDbCommand 的函式。For a list of these values, see the OleDbCommand constructor.

OleDbCommand 的功能,是在資料來源執行命令的下列方法:OleDbCommand features the following methods executing commands at a data source:

項目Item 說明Description
ExecuteReader 執行傳回資料列的命令。Executes commands that return rows. 如果用來執行 SQL SET 語句之類的命令,ExecuteReader 可能不會有您想要的效果。ExecuteReader may not have the effect that you want if used to execute commands such as SQL SET statements.
ExecuteNonQuery 執行 SQL INSERT、DELETE、UPDATE 和 SET 語句之類的命令。Executes commands such as SQL INSERT, DELETE, UPDATE, and SET statements.
ExecuteScalar 抓取單一值,例如,資料庫中的匯總值。Retrieves a single value, for example, an aggregate value from a database.

您可以重設 CommandText 屬性,並重複使用 OleDbCommand 物件。You can reset the CommandText property and reuse the OleDbCommand object. 不過,您必須先關閉 OleDbDataReader,才能執行新的或先前的命令。However, you must close the OleDbDataReader before you can execute a new or previous command.

如果嚴重的 OleDbException (例如,SQL Server 嚴重性層級20或更大)是由執行 OleDbCommand的方法所產生,則 OleDbConnection,連接可能會關閉。If a fatal OleDbException (for example, a SQL Server severity level of 20 or greater) is generated by the method executing an OleDbCommand, the OleDbConnection, the connection may be closed. 但是,使用者可以再次開啟連線,然後繼續進行。However, the user can reopen the connection and continue.

建構函式

OleDbCommand()

初始化 OleDbCommand 類別的新執行個體。Initializes a new instance of the OleDbCommand class.

OleDbCommand(String)

使用查詢的文字,初始化 OleDbCommand 類別的新執行個體。Initializes a new instance of the OleDbCommand class with the text of the query.

OleDbCommand(String, OleDbConnection)

使用查詢的文字和 OleDbConnection 初始化 OleDbCommand 類別的新執行個體。Initializes a new instance of the OleDbCommand class with the text of the query and an OleDbConnection.

OleDbCommand(String, OleDbConnection, OleDbTransaction)

使用查詢的文字、OleDbConnectionTransaction,初始化 OleDbCommand 類別的新執行個體。Initializes a new instance of the OleDbCommand class with the text of the query, an OleDbConnection, and the Transaction.

屬性

CanRaiseEvents

取得值,指出元件是否能引發事件。Gets a value indicating whether the component can raise an event.

(繼承來源 Component)
CommandText

取得或設定要在資料來源執行的 SQL 陳述式或預存程序。Gets or sets the SQL statement or stored procedure to execute at the data source.

CommandTimeout

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

CommandType

取得或設定值,這個值表示如何解譯 CommandText 屬性。Gets or sets a value that indicates how the CommandText property is interpreted.

Connection

取得或設定 OleDbCommand 的這個執行個體所使用的 OleDbConnectionGets or sets the OleDbConnection used by this instance of the OleDbCommand.

Container

取得包含 IContainerComponentGets the IContainer that contains the Component.

(繼承來源 Component)
DesignMode

取得值,指出 Component 目前是否處於設計模式。Gets a value that indicates whether the Component is currently in design mode.

(繼承來源 Component)
DesignTimeVisible

取得或設定值,指出在命令物件是否應該顯示在自訂 Windows Form 設計工具控制項中。Gets or sets a value that indicates whether the command object should be visible in a customized Windows Forms Designer control.

Events

取得附加在這個 Component 上的事件處理常式清單。Gets the list of event handlers that are attached to this Component.

(繼承來源 Component)
Parameters

取得 OleDbParameterCollectionGets the OleDbParameterCollection.

Site

取得或設定 ComponentISiteGets or sets the ISite of the Component.

(繼承來源 Component)
Transaction

取得或設定在其中執行 OleDbTransactionOleDbCommandGets or sets the OleDbTransaction within which the OleDbCommand executes.

UpdatedRowSource

取得或設定當由 DataRowUpdate 方法使用命令結果時,如何套用至 OleDbDataAdapterGets or sets how command results are applied to the DataRow when used by the Update method of the OleDbDataAdapter.

方法

Cancel()

嘗試取消 OleDbCommand 的執行。Tries to cancel the execution of an OleDbCommand.

Clone()

建立目前執行個體複本的新 OleDbCommand 物件。Creates a new OleDbCommand object that is a copy of the current instance.

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(繼承來源 MarshalByRefObject)
CreateParameter()

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

Dispose()

釋放 Component 所使用的所有資源。Releases all resources used by the Component.

(繼承來源 Component)
Dispose(Boolean)

釋放 Component 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。Releases the unmanaged resources used by the Component and optionally releases the managed resources.

(繼承來源 Component)
Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
ExecuteNonQuery()

針對 Connection 執行 SQL 陳述式,並傳回受影響的資料列數。Executes an SQL statement against the Connection and returns the number of rows affected.

ExecuteReader()

傳送 CommandTextConnection,並建置 OleDbDataReaderSends the CommandText to the Connection and builds an OleDbDataReader.

ExecuteReader(CommandBehavior)

傳送 CommandTextConnection,並使用其中一個 CommandBehavior 值來建立 OleDbDataReaderSends the CommandText to the Connection, and builds an OleDbDataReader using one of the CommandBehavior values.

ExecuteScalar()

執行查詢,並傳回查詢所傳回的結果集第一個資料列的第一個資料行。Executes the query, and returns the first column of the first row in the result set returned by the query. 會忽略其他的資料行或資料列。Additional columns or rows are ignored.

GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetLifetimeService()

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
GetService(Type)

傳回表示 Component 或其 Container 所提供之服務的物件。Returns an object that represents a service provided by the Component or by its Container.

(繼承來源 Component)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
InitializeLifetimeService()

取得存留期服務物件,以控制這個執行個體的存留期原則。Obtains a lifetime service object to control the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。Creates a shallow copy of the current MarshalByRefObject object.

(繼承來源 MarshalByRefObject)
Prepare()

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

ResetCommandTimeout()

重設 CommandTimeout 屬性為預設值。Resets the CommandTimeout property to the default value.

ToString()

傳回任何包含 Component 名稱的 StringReturns a String containing the name of the Component, if any. 不應覆寫此方法。This method should not be overridden.

(繼承來源 Component)

事件

Disposed

當此元件由 Dispose() 方法的呼叫處置時發生。Occurs when the component is disposed by a call to the Dispose() method.

(繼承來源 Component)

明確介面實作

ICloneable.Clone()

如需這個成員的說明,請參閱 Clone()For a description of this member, see Clone().

IDbCommand.CreateParameter()

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

IDbCommand.ExecuteReader()

如需這個成員的說明,請參閱 ExecuteReader()For a description of this member, see ExecuteReader().

IDbCommand.ExecuteReader(CommandBehavior)

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

適用於

另請參閱