OdbcCommand OdbcCommand OdbcCommand OdbcCommand Class

定义

表示要对数据源执行的 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
继承
实现

示例

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

Item 描述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.

如果执行此命令会导致严重OdbcExceptionSQL 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() OdbcCommand() OdbcCommand()

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

OdbcCommand(String) OdbcCommand(String) OdbcCommand(String) OdbcCommand(String)

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

OdbcCommand(String, OdbcConnection) OdbcCommand(String, OdbcConnection) OdbcCommand(String, OdbcConnection) 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(String, OdbcConnection, OdbcTransaction) OdbcCommand(String, OdbcConnection, OdbcTransaction) OdbcCommand(String, OdbcConnection, OdbcTransaction)

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

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

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

获取或设置一个值,指示命令对象是否应在自定义的界面控件中可见。Gets or sets a value that indicates whether the command object should be visible in a customized interface 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

获取 OdbcParameterCollectionGets the OdbcParameterCollection.

Site Site Site Site

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

(Inherited from Component)
Transaction Transaction Transaction Transaction

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

UpdatedRowSource UpdatedRowSource UpdatedRowSource UpdatedRowSource

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

方法

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

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

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

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

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

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

将发送CommandTextConnection,并生成OdbcDataReaderSends the CommandText to the Connection and builds an OdbcDataReader.

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

将发送CommandTextConnection,并生成OdbcDataReader使用之一CommandBehavior值。Sends the CommandText to the Connection, and builds an OdbcDataReader 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 at 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()
IDbCommand.ExecuteReader() IDbCommand.ExecuteReader() IDbCommand.ExecuteReader() IDbCommand.ExecuteReader()

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

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

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

事件

Disposed Disposed Disposed Disposed

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

(Inherited from Component)

适用于

另请参阅