OleDbCommand OleDbCommand OleDbCommand OleDbCommand Class


表示要对数据源执行的 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. 已填充DataSet然后返回。The 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);
        OleDbDataReader reader = command.ExecuteReader();

        while (reader.Read())
            Console.WriteLine(reader.GetInt32(0) + ", " + reader.GetString(1));
        // always call Close when done reading.
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)


        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.
    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. ExecuteReader 可能想要使用的效果来执行命令,例如 SQL SET 语句。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() OleDbCommand() OleDbCommand()

初始化 OleDbCommand 类的新实例。Initializes a new instance of the OleDbCommand class.

OleDbCommand(String) OleDbCommand(String) OleDbCommand(String) OleDbCommand(String)

使用查询的文本初始化 OleDbCommand 类的新实例。Initializes a new instance of the OleDbCommand class with the text of the query.

OleDbCommand(String, OleDbConnection) OleDbCommand(String, OleDbConnection) OleDbCommand(String, OleDbConnection) 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) OleDbCommand(String, OleDbConnection, OleDbTransaction) OleDbCommand(String, OleDbConnection, OleDbTransaction) 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.


CanRaiseEvents CanRaiseEvents CanRaiseEvents CanRaiseEvents

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

(Inherited from Component)
CommandText CommandText CommandText CommandText

获取或设置要对数据源执行的 SQL 语句或存储过程。Gets or sets the SQL statement or stored procedure to execute at the data source.

CommandTimeout CommandTimeout CommandTimeout CommandTimeout

获取或设置在终止对执行命令的尝试并生成错误之前的等待时间。Gets or sets the wait time before terminating an attempt to execute a command and generating an error.

CommandType CommandType CommandType CommandType

获取或设置一个值,该值指示如何解释 CommandText 属性。Gets or sets a value that indicates how the CommandText property is interpreted.

Connection Connection Connection Connection

获取或设置 OleDbCommand 的此实例使用的 OleDbConnectionGets or sets the OleDbConnection used by this instance of the OleDbCommand.

Container Container Container Container

获取 IContainer,它包含 ComponentGets the IContainer that contains the Component.

(Inherited from Component)
DesignMode DesignMode DesignMode DesignMode

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

(Inherited from Component)
DesignTimeVisible DesignTimeVisible DesignTimeVisible DesignTimeVisible

获取或设置一个值,该值指示命令对象在自定义的 Windows 窗体设计器控件中是否应可见。Gets or sets a value that indicates whether the command object should be visible in a customized Windows Forms Designer control.

Events Events Events Events

获取附加到此 Component 的事件处理程序的列表。Gets the list of event handlers that are attached to this Component.

(Inherited from Component)
Parameters Parameters Parameters Parameters

获取 OleDbParameterCollectionGets the OleDbParameterCollection.

Site Site Site Site

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

(Inherited from Component)
Transaction Transaction Transaction Transaction

获取或设置要在其中执行 OleDbTransactionOleDbCommandGets or sets the OleDbTransaction within which the OleDbCommand executes.

UpdatedRowSource UpdatedRowSource UpdatedRowSource UpdatedRowSource

获取或设置命令结果在由 DataRowUpdate 方法使用时如何应用于 OleDbDataAdapterGets or sets how command results are applied to the DataRow when used by the Update method of the OleDbDataAdapter.


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

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

Clone() Clone() Clone() Clone()

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

CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type) 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() CreateParameter() CreateParameter() CreateParameter()

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

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

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

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

释放由 Component 占用的非托管资源,还可以另外再释放托管资源。Releases the unmanaged resources used by the Component and optionally releases the managed resources.

(Inherited from Component)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
ExecuteNonQuery() ExecuteNonQuery() ExecuteNonQuery() ExecuteNonQuery()

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

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

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

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

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

ExecuteScalar() ExecuteScalar() ExecuteScalar() 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() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Object)
GetLifetimeService() GetLifetimeService() GetLifetimeService() GetLifetimeService()

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

(Inherited from MarshalByRefObject)
GetService(Type) GetService(Type) GetService(Type) GetService(Type)

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

(Inherited from Component)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService()

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

(Inherited from MarshalByRefObject)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean)

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

(Inherited from MarshalByRefObject)
Prepare() Prepare() Prepare() Prepare()

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

ResetCommandTimeout() ResetCommandTimeout() ResetCommandTimeout() ResetCommandTimeout()

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

ToString() ToString() ToString() ToString()

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

(Inherited from Component)


ICloneable.Clone() ICloneable.Clone() ICloneable.Clone() ICloneable.Clone()

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

IDbCommand.CreateParameter() IDbCommand.CreateParameter() IDbCommand.CreateParameter() IDbCommand.CreateParameter()

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

IDbCommand.ExecuteReader() IDbCommand.ExecuteReader() IDbCommand.ExecuteReader() IDbCommand.ExecuteReader()

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

IDbCommand.ExecuteReader(CommandBehavior) IDbCommand.ExecuteReader(CommandBehavior) IDbCommand.ExecuteReader(CommandBehavior) IDbCommand.ExecuteReader(CommandBehavior)

Connection 执行 CommandText,并使用 CommandBehavior 值之一生成 IDataReaderExecutes the CommandText against the Connection, and builds an IDataReader using one of the CommandBehavior values.


Disposed Disposed Disposed Disposed

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

(Inherited from Component)