OleDbCommand Class

Definition

表示要对数据源执行的 SQL 语句或存储过程。Represents an SQL statement or stored procedure to execute against a data source.

public ref class OleDbCommand sealed : System::Data::Common::DbCommand, ICloneable
public ref class OleDbCommand sealed : System::ComponentModel::Component, ICloneable, IDisposable, System::Data::IDbCommand
public ref class OleDbCommand sealed : System::Data::Common::DbCommand, ICloneable, IDisposable
public sealed class OleDbCommand : System.Data.Common.DbCommand, ICloneable
public sealed class OleDbCommand : System.ComponentModel.Component, ICloneable, IDisposable, System.Data.IDbCommand
public sealed class OleDbCommand : System.Data.Common.DbCommand, ICloneable, IDisposable
type OleDbCommand = class
    inherit DbCommand
    interface IDbCommand
    interface IDisposable
    interface ICloneable
type OleDbCommand = class
    inherit Component
    interface ICloneable
    interface IDbCommand
    interface IDisposable
type OleDbCommand = class
    inherit DbCommand
    interface ICloneable
    interface IDbCommand
    interface IDisposable
Public NotInheritable Class OleDbCommand
Inherits DbCommand
Implements ICloneable
Public NotInheritable Class OleDbCommand
Inherits Component
Implements ICloneable, IDbCommand, IDisposable
Public NotInheritable Class OleDbCommand
Inherits DbCommand
Implements ICloneable, IDisposable
Inheritance
OleDbCommand
Inheritance
Inheritance
Implements

Examples

下面的示例使用 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

Remarks

当创建 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.

Constructors

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)

使用查询文本、OleDbConnection 以及 Transaction 初始化 OleDbCommand 类的新实例。Initializes a new instance of the OleDbCommand class with the text of the query, an OleDbConnection, and the Transaction.

Properties

CanRaiseEvents

获取一个指示组件是否可以引发事件的值。Gets a value indicating whether the component can raise an event.

(Inherited from 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.

(Inherited from Component)
DbConnection

当在派生类中重写时,获取或设置此 DbCommand 使用的 DbConnectionWhen overridden in a derived class, gets or sets the DbConnection used by this DbCommand.

(Inherited from DbCommand)
DbParameterCollection

在派生类中重写时,获取 DbParameter 对象的集合。When overridden in a derived class, gets the collection of DbParameter objects.

(Inherited from DbCommand)
DbTransaction

当在派生类中重写时,获取或设置执行 DbCommand 对象时所在的 DbTransactionWhen overridden in a derived class, gets or sets the DbTransaction within which this DbCommand object executes.

(Inherited from DbCommand)
DesignMode

获取一个值,用以指示 Component 当前是否处于设计模式。Gets a value that indicates whether the Component is currently in design mode.

(Inherited from Component)
DesignTimeVisible

获取或设置一个值,该值指示命令对象在自定义的 Windows 窗体设计器控件中是否应可见。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.

(Inherited from Component)
Parameters

获取 OleDbParameterCollectionGets the OleDbParameterCollection.

Site

获取或设置 ComponentISiteGets or sets the ISite of the Component.

(Inherited from 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.

Methods

Cancel()

尝试取消执行 OleDbCommandTries to cancel the execution of an OleDbCommand.

Clone()

创建是当前实例的副本的新 OleDbCommand 对象。Creates a new OleDbCommand object that is a copy of the current instance.

CreateDbParameter()

在派生类中重写时,创建 DbParameter 对象的新实例。When overridden in a derived class, creates a new instance of a DbParameter object.

(Inherited from DbCommand)
CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
CreateParameter()

创建 OleDbParameter 对象的新实例。Creates a new instance of an OleDbParameter object.

Dispose() (Inherited from DbCommand)
Dispose()

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

(Inherited from Component)
Dispose(Boolean) (Inherited from DbCommand)
Dispose(Boolean)

释放 Component 使用的非托管资源,并选择性地释放托管资源。Releases the unmanaged resources used by the Component and optionally releases the managed resources.

(Inherited from Component)
DisposeAsync()

异步处置命令对象。Asynchronously diposes the command object.

(Inherited from DbCommand)
Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
ExecuteDbDataReader(CommandBehavior)

当在派生类中重写时,针对连接执行命令文本。When overridden in a derived class, executes the command text against the connection.

(Inherited from DbCommand)
ExecuteDbDataReaderAsync(CommandBehavior, CancellationToken)

提供程序应执行该方法对 ExecuteReader 重载提供非默认的实现。Providers should implement this method to provide a non-default implementation for ExecuteReader overloads.

默认实现调用同步 ExecuteReader() 方法并返回已完成任务,以便阻止调用线程。The default implementation invokes the synchronous ExecuteReader() method and returns a completed task, blocking the calling thread. 如果传递到已取消的取消标记,则默认实现将返回已取消的任务。The default implementation will return a cancelled task if passed an already cancelled cancellation token. ExecuteReader 引发的异常将通过返回的任务异常属性传递。Exceptions thrown by ExecuteReader will be communicated via the returned Task Exception property.

该方法可用于请求操作之前接受取消标记。This method accepts a cancellation token that can be used to request the operation to be cancelled early. 实现可能会忽略该请求。Implementations may ignore this request.

(Inherited from DbCommand)
ExecuteNonQuery()

针对 Connection 执行 SQL 语句并返回受影响的行数。Executes an SQL statement against the Connection and returns the number of rows affected.

ExecuteNonQueryAsync()

ExecuteNonQuery()的异步版本,针对一个连接对象执行一个 SQL 声明。An asynchronous version of ExecuteNonQuery(), which executes a SQL statement against a connection object.

通过 CancellationToken.None 调用 ExecuteNonQueryAsync(CancellationToken)Invokes ExecuteNonQueryAsync(CancellationToken) with CancellationToken.None.

(Inherited from DbCommand)
ExecuteNonQueryAsync(CancellationToken)

这是 ExecuteNonQuery() 的异步版本。This is the asynchronous version of ExecuteNonQuery(). 提供程序应使用合适的实现进行重写。Providers should override with an appropriate implementation. 可选择性忽略取消标记。The cancellation token may optionally be ignored.

默认实现调用同步 ExecuteNonQuery() 方法并返回已完成任务,以便阻止调用线程。The default implementation invokes the synchronous ExecuteNonQuery() method and returns a completed task, blocking the calling thread. 如果传递到已取消的取消标记,则默认实现将返回已取消的任务。The default implementation will return a cancelled task if passed an already cancelled cancellation token. ExecuteNonQuery() 引发的异常将通过任务异常属性传递。Exceptions thrown by ExecuteNonQuery() will be communicated via the returned Task Exception property.

在返回的任务完成前,不要调用 DbCommand 对象的其他方法和属性。Do not invoke other methods and properties of the DbCommand object until the returned Task is complete.

(Inherited from DbCommand)
ExecuteReader()

CommandText 发送到 Connection 并生成一个 OleDbDataReaderSends the CommandText to the Connection and builds an OleDbDataReader.

ExecuteReader(CommandBehavior)

CommandText 发送到 Connection,并使用 CommandBehavior 值之一构造 OleDbDataReaderSends the CommandText to the Connection, and builds an OleDbDataReader using one of the CommandBehavior values.

ExecuteReaderAsync()

ExecuteReader 的异步版本,针对 CommandTextConnection 执行 DbDataReaderAn asynchronous version of ExecuteReader, which executes the CommandText against the Connection and returns a DbDataReader.

通过 CancellationToken.None 调用 ExecuteDbDataReaderAsync(CommandBehavior, CancellationToken)Invokes ExecuteDbDataReaderAsync(CommandBehavior, CancellationToken) with CancellationToken.None.

(Inherited from DbCommand)
ExecuteReaderAsync(CancellationToken)

ExecuteReader 的异步版本,针对 CommandTextConnection 执行 DbDataReaderAn asynchronous version of ExecuteReader, which executes the CommandText against the Connection and returns a DbDataReader. 此方法传播有关应取消操作的通知。This method propagates a notification that operations should be canceled.

调用 ExecuteDbDataReaderAsync(CommandBehavior, CancellationToken).Invokes ExecuteDbDataReaderAsync(CommandBehavior, CancellationToken).

(Inherited from DbCommand)
ExecuteReaderAsync(CommandBehavior)

ExecuteReader 的异步版本,针对 CommandTextConnection 执行 DbDataReaderAn asynchronous version of ExecuteReader, which executes the CommandText against the Connection and returns a DbDataReader.

调用 ExecuteDbDataReaderAsync(CommandBehavior, CancellationToken).Invokes ExecuteDbDataReaderAsync(CommandBehavior, CancellationToken).

(Inherited from DbCommand)
ExecuteReaderAsync(CommandBehavior, CancellationToken)

调用 ExecuteDbDataReaderAsync(CommandBehavior, CancellationToken).Invokes ExecuteDbDataReaderAsync(CommandBehavior, CancellationToken).

(Inherited from DbCommand)
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.

ExecuteScalarAsync()

ExecuteScalar() 的异步版本,执行查询,并将查询返回的结果集中第一行的第一列返回。An asynchronous version of ExecuteScalar(), which executes the query and returns the first column of the first row in the result set returned by the query. 所有其他的列和行将被忽略。All other columns and rows are ignored.

通过 CancellationToken.None 调用 ExecuteScalarAsync(CancellationToken)Invokes ExecuteScalarAsync(CancellationToken) with CancellationToken.None.

(Inherited from DbCommand)
ExecuteScalarAsync(CancellationToken)

这是 ExecuteScalar() 的异步版本。This is the asynchronous version of ExecuteScalar(). 提供程序应使用合适的实现进行重写。Providers should override with an appropriate implementation. 可选择性忽略取消标记。The cancellation token may optionally be ignored.

默认实现调用同步 ExecuteScalar() 方法并返回已完成任务,以便阻止调用线程。The default implementation invokes the synchronous ExecuteScalar() method and returns a completed task, blocking the calling thread. 如果传递到已取消的取消标记,则默认实现将返回已取消的任务。The default implementation will return a cancelled task if passed an already cancelled cancellation token. ExecuteScalar 引发的异常将通过返回的任务异常属性传递。Exceptions thrown by ExecuteScalar will be communicated via the returned Task Exception property.

在返回的任务完成前,不要调用 DbCommand 对象的其他方法和属性。Do not invoke other methods and properties of the DbCommand object until the returned Task is complete.

(Inherited from DbCommand)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetService(Type)

返回一个对象,该对象表示由 Component 或它的 Container 提供的服务。Returns an object that represents a service provided by the Component or by its Container.

(Inherited from Component)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
Prepare()

在数据源上创建准备就绪(或已编译)的命令版本。Creates a prepared (or compiled) version of the command on the data source.

PrepareAsync(CancellationToken)

在数据源上异步创建准备就绪(或已编译)的命令版本。Asynchronously creates a prepared (or compiled) version of the command on the data source.

(Inherited from DbCommand)
ResetCommandTimeout()

CommandTimeout 属性重置为默认值。Resets the CommandTimeout property to the default value.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)
ToString()

返回包含 Component 的名称的 String(如果有)。Returns a String containing the name of the Component, if any. 不应重写此方法。This method should not be overridden.

(Inherited from Component)

Events

Disposed

在通过调用 Dispose() 方法释放组件时发生。Occurs when the component is disposed by a call to the Dispose() method.

(Inherited from Component)

Explicit Interface Implementations

ICloneable.Clone()

有关此成员的说明,请参见 Clone()For a description of this member, see Clone().

IDbCommand.Connection

获取或设置 IDbCommand 的此实例使用的 IDbConnectionGets or sets the IDbConnection used by this instance of the IDbCommand.

(Inherited from DbCommand)
IDbCommand.CreateParameter()

创建 IDbDataParameter 对象的新实例。Creates a new instance of an IDbDataParameter object.

IDbCommand.CreateParameter()

创建 IDbDataParameter 对象的新实例。Creates a new instance of an IDbDataParameter object.

(Inherited from DbCommand)
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.

IDbCommand.Parameters

获取 IDataParameterCollectionGets the IDataParameterCollection.

(Inherited from DbCommand)
IDbCommand.Transaction

获取或设置将在其中执行此 DbCommand 对象的 DbTransactionGets or sets the DbTransaction within which this DbCommand object executes.

(Inherited from DbCommand)

Applies to

See also