OdbcCommand 类

表示要对数据源执行的 SQL 语句或存储过程。无法继承此类。

**命名空间:**System.Data.Odbc
**程序集:**System.Data(在 system.data.dll 中)

语法

声明
Public NotInheritable Class OdbcCommand
    Inherits DbCommand
    Implements ICloneable
用法
Dim instance As OdbcCommand
public sealed class OdbcCommand : DbCommand, ICloneable
public ref class OdbcCommand sealed : public DbCommand, ICloneable
public final class OdbcCommand extends DbCommand implements ICloneable
public final class OdbcCommand extends DbCommand implements ICloneable

备注

OdbcCommand 类提供下列对数据源执行命令的方法:

说明

ExecuteReader

执行返回行的命令。

ExecuteNonQuery

执行 SQL INSERT、DELETE、UPDATE 和 SET 语句等命令。

ExecuteScalar

从数据库中检索单个值(例如一个聚合值)。

您可以重置 CommandText 属性并重复使用 OdbcCommand 对象。但是,在执行新的命令或先前命令之前,必须关闭 OdbcDataReader

如果执行命令引起致命的 OdbcException(例如,SQL Server 严重级别为 20 或更高),则 OdbcConnection 可能会关闭。但是,用户可以重新打开连接并继续操作。

示例

下面的示例使用 OdbcCommand 类的 ExecuteReader 方法以及 OdbcDataReaderOdbcConnection 类从表中选择行。

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
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.
    }

继承层次结构

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Data.Common.DbCommand
        System.Data.Odbc.OdbcCommand

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1

请参见

参考

OdbcCommand 成员
System.Data.Odbc 命名空间
OdbcDataAdapter
OdbcConnection

其他资源

Executing a Command