OdbcCommand 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示要针对数据源执行的 SQL 语句或存储过程。 无法继承此类。
public ref class OdbcCommand sealed : System::Data::Common::DbCommand, ICloneablepublic ref class OdbcCommand sealed : System::ComponentModel::Component, ICloneable, IDisposable, System::Data::IDbCommandpublic sealed class OdbcCommand : System.Data.Common.DbCommand, ICloneablepublic sealed class OdbcCommand : System.ComponentModel.Component, ICloneable, IDisposable, System.Data.IDbCommandtype OdbcCommand = class
    inherit DbCommand
    interface ICloneabletype OdbcCommand = class
    inherit Component
    interface ICloneable
    interface IDbCommand
    interface IDisposablePublic NotInheritable Class OdbcCommand
Inherits DbCommand
Implements ICloneablePublic NotInheritable Class OdbcCommand
Inherits Component
Implements ICloneable, IDbCommand, IDisposable- 继承
- 继承
- 实现
示例
以下示例使用 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 类提供以下方法,用于针对数据源执行命令:
| 项目 | 描述 | 
|---|---|
| ExecuteReader | 执行返回行的命令。 | 
| ExecuteNonQuery | 执行 SQL INSERT、DELETE、UPDATE 和 SET 语句等命令。 | 
| ExecuteScalar | 从数据库检索单个值,例如聚合值。 | 
可以重置 CommandText 属性并重复使用 OdbcCommand 对象。 但是,必须先关闭 OdbcDataReader,然后才能执行新的或以前的命令。
如果执行命令会导致致命 OdbcException(例如 SQL Server 严重级别为 20 或更高),OdbcConnection 可能会关闭。 但是,用户可以重新打开连接并继续。
构造函数
| OdbcCommand() | 初始化 OdbcCommand 类的新实例。 | 
| OdbcCommand(String) | 使用查询的文本初始化 OdbcCommand 类的新实例。 | 
| OdbcCommand(String, OdbcConnection) | 使用查询的文本和 OdbcConnection 对象初始化 OdbcCommand 类的新实例。 | 
| OdbcCommand(String, OdbcConnection, OdbcTransaction) | 使用查询文本、OdbcConnection 对象和 Transaction初始化 OdbcCommand 类的新实例。 | 
属性
| CanRaiseEvents | 获取一个值,该值指示组件是否可以引发事件。(继承自 Component) | 
| CommandText | 获取或设置要针对数据源执行的 SQL 语句或存储过程。 | 
| CommandTimeout | 获取或设置在终止尝试执行命令并生成错误之前(以秒为单位)的等待时间。 | 
| CommandType | 获取或设置一个值,该值指示如何解释 CommandText 属性。 | 
| Connection | 获取或设置此 OdbcCommand实例使用的 OdbcConnection。 | 
| Container | 获取包含 Component的 IContainer。(继承自 Component) | 
| DbConnection | 获取或设置此 DbCommand使用的 DbConnection。(继承自 DbCommand) | 
| DbParameterCollection | 获取 DbParameter 对象的集合。(继承自 DbCommand) | 
| DbTransaction | 获取或设置执行此 DbCommand 对象的 DbTransaction。(继承自 DbCommand) | 
| DesignMode | 获取一个值,该值指示 Component 当前是否处于设计模式。(继承自 Component) | 
| DesignTimeVisible | 获取或设置一个值,该值指示命令对象是否应在自定义接口控件中可见。 | 
| Events | 获取附加到此 Component的事件处理程序的列表。(继承自 Component) | 
| Parameters | |
| Site | (继承自 Component) | 
| Transaction | 获取或设置执行 OdbcCommandOdbcTransaction。 | 
| UpdatedRowSource | 获取或设置一个值,该值指定 Update 方法如何将命令结果应用于 DataRow。 | 
方法
事件
| Disposed | 当组件通过调用 Dispose() 方法释放时发生。(继承自 Component) | 
显式接口实现
| ICloneable.Clone() | 有关此成员的说明,请参阅 Clone()。 | 
| IDbCommand.Connection | 获取或设置此 IDbCommand实例使用的 IDbConnection。(继承自 DbCommand) | 
| IDbCommand.CreateParameter() | 创建 IDbDataParameter 对象的新实例。 | 
| IDbCommand.CreateParameter() | 创建 IDbDataParameter 对象的新实例。(继承自 DbCommand) | 
| IDbCommand.ExecuteReader() | 此 API 支持产品基础结构,不能在代码中直接使用。 针对 Connection 执行 CommandText 并生成 IDataReader。 | 
| IDbCommand.ExecuteReader() | 针对 Connection 执行 CommandText 并生成 IDataReader。(继承自 DbCommand) | 
| IDbCommand.ExecuteReader(CommandBehavior) | 此 API 支持产品基础结构,不能在代码中直接使用。 针对 Connection执行 CommandText,并使用指定的行为生成 IDataReader。 | 
| IDbCommand.ExecuteReader(CommandBehavior) | 针对 Connection执行 CommandText,并使用其中一个 CommandBehavior 值生成 IDataReader。(继承自 DbCommand) | 
| IDbCommand.Parameters | (继承自 DbCommand) | 
| IDbCommand.Transaction | 获取或设置执行此 DbCommand 对象的 DbTransaction。(继承自 DbCommand) | 
扩展方法
| ConfigureAwait(IAsyncDisposable, Boolean) | 配置如何执行从异步可释放项返回的任务的 await。 |