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)

使用查询文本、OleDbConnection 以及 Transaction 初始化 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

获取 IContainer,它包含 ComponentGets 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 窗体设计器控件中是否应可见。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()

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

Clone()

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

CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。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 占用的非托管资源,还可以另外再释放托管资源。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()

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.

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

检索控制此实例的生存期策略的当前生存期服务对象。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 的浅表副本。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 的名称的 String(如果有)。Returns 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.

适用于

另请参阅