OdbcCommand 类

定义

表示要对数据源执行的 SQL 语句或存储过程。Represents an SQL statement or stored procedure to execute against a data source. 无法继承此类。This class cannot be inherited.

public ref class OdbcCommand sealed : System::Data::Common::DbCommand, ICloneable
public sealed class OdbcCommand : System.Data.Common.DbCommand, ICloneable
type OdbcCommand = class
    inherit DbCommand
    interface ICloneable
Public NotInheritable Class OdbcCommand
Inherits DbCommand
Implements ICloneable
继承
OdbcCommand
继承
继承
实现

示例

下面的示例使用 ExecuteNonQueryThe following example uses ExecuteNonQuery.

public void InsertRow(string connectionString, string insertSQL)
{
    using (OdbcConnection connection = 
               new OdbcConnection(connectionString))
    {
        // The insertSQL string contains a SQL statement that
        // inserts a new row in the source table.
        OdbcCommand command = new OdbcCommand(insertSQL, connection);

        // Open the connection and execute the insert command.
        try
        {
            connection.Open();
            command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        // The connection is automatically closed when the
        // code exits the using block.
    }
Public Sub InsertRow(ByVal connectionString As String, _
    ByVal insertSQL As String)

    Using connection As New OdbcConnection(connectionString)
        ' The insertSQL string contains a SQL statement that
        ' inserts a new row in the source table.
        Dim command As New OdbcCommand(insertSQL, connection)

        ' Open the connection and execute the insert command.
        Try
            connection.Open()
            command.ExecuteNonQuery()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
        ' The connection is automatically closed when the
        ' code exits the Using block.
    End Using
End Sub

注解

OdbcCommand 类提供以下方法来针对数据源执行命令:The OdbcCommand class provides the following methods for executing commands against a data source:

ItemItem 说明Description
ExecuteReader 执行返回行的命令。Executes commands that return rows.
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 属性,并重新使用 OdbcCommand 对象。You can reset the CommandText property and reuse the OdbcCommand object. 但是,必须先关闭 OdbcDataReader,然后才能执行新的或前一个命令。However, you must close the OdbcDataReader before you can execute a new or previous command.

如果执行命令导致严重 OdbcException 如 SQL Server 严重级别为20或更大,则 OdbcConnection 可能会关闭。If execution of the command causes a fatal OdbcException such as a SQL Server severity level of 20 or more, OdbcConnection may close. 但是,用户可以重新打开连接并继续操作。However, the user can reopen the connection and continue.

构造函数

OdbcCommand()

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

OdbcCommand(String)

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

OdbcCommand(String, OdbcConnection)

用查询文本和 OdbcCommand 对象初始化 OdbcConnection 类的新实例。Initializes a new instance of the OdbcCommand class with the text of the query and an OdbcConnection object.

OdbcCommand(String, OdbcConnection, OdbcTransaction)

使用查询文本、OdbcCommand 对象以及 OdbcConnection 初始化 Transaction 类的新实例。Initializes a new instance of the OdbcCommand class with the text of the query, an OdbcConnection object, 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 against 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

获取或设置 OdbcConnection 的此实例使用的 OdbcCommandGets or sets the OdbcConnection used by this instance of the OdbcCommand.

Container

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

(继承自 Component)
DbConnection

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

(继承自 DbCommand)
DbParameterCollection

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

(继承自 DbCommand)
DbTransaction

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

(继承自 DbCommand)
DesignMode

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

(继承自 Component)
DesignTimeVisible

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

Events

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

(继承自 Component)
Parameters

获取 OdbcParameterCollectionGets the OdbcParameterCollection.

Site

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

(继承自 Component)
Transaction

获取或设置要在其中执行 OdbcTransactionOdbcCommandGets or sets the OdbcTransaction within which the OdbcCommand executes.

UpdatedRowSource

获取或设置一个值,该值指定 “Update” 方法应如何将命令结果应用于“DataRow”。Gets or sets a value that specifies how the Update method should apply command results to the DataRow.

方法

Cancel()

尝试取消执行 OdbcCommandTries to cancel the execution of an OdbcCommand.

CreateDbParameter()

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

(继承自 DbCommand)
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()

创建 OdbcParameter 对象的新实例。Creates a new instance of an OdbcParameter 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)
DisposeAsync()

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

(继承自 DbCommand)
Equals(Object)

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

(继承自 Object)
ExecuteDbDataReader(CommandBehavior)

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

(继承自 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.

(继承自 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.

(继承自 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.

(继承自 DbCommand)
ExecuteReader()

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

ExecuteReader(CommandBehavior)

CommandText 发送到 Connection,并使用 OdbcDataReader 值之一构造 CommandBehaviorSends the CommandText to the Connection, and builds an OdbcDataReader 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.

(继承自 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).

(继承自 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).

(继承自 DbCommand)
ExecuteReaderAsync(CommandBehavior, CancellationToken)

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

(继承自 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.

(继承自 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.

(继承自 DbCommand)
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 at the data source.

PrepareAsync(CancellationToken)

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

(继承自 DbCommand)
ResetCommandTimeout()

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

ToString()

返回包含 String 的名称的 Component(如果有)。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.Connection

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

(继承自 DbCommand)
IDbCommand.CreateParameter()
IDbCommand.ExecuteReader()

CommandText 执行 Connection 并生成 IDataReaderExecutes the CommandText against the Connection and builds an IDataReader.

IDbCommand.ExecuteReader(CommandBehavior)

针对 CommandText 执行 Connection,并使用指定行为生成 IDataReaderExecutes the CommandText against the Connection, and builds an IDataReader using the specified behavior.

IDbCommand.Parameters

获取 IDataParameterCollectionGets the IDataParameterCollection.

(继承自 DbCommand)
IDbCommand.Transaction

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

(继承自 DbCommand)

适用于

另请参阅