IDbCommand 接口

定义

表示连接到数据源时执行的 SQL 语句,并由访问关系数据库的 .NET Framework 数据提供程序实现。Represents an SQL statement that is executed while connected to a data source, and is implemented by .NET Framework data providers that access relational databases.

public interface class IDbCommand : IDisposable
public interface IDbCommand : IDisposable
type IDbCommand = interface
    interface IDisposable
Public Interface IDbCommand
Implements IDisposable
派生
实现

示例

下面的示例创建派生类的实例,SqlConnectionSqlCommandSqlDataReaderThe following example creates instances of the derived classes, SqlConnection, SqlCommand, and SqlDataReader. 该示例将读取数据,并将数据写入控制台。The example reads through the data, writing it to the console. 最后,该示例关闭 SqlDataReader,然后 SqlConnectionFinally, the example closes the SqlDataReader, then the SqlConnection.

private static void ReadOrderData(string connectionString)
{
    string queryString = 
        "SELECT OrderID, CustomerID FROM dbo.Orders;";
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(
            queryString, connection);
        connection.Open();
        using(SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                Console.WriteLine(String.Format("{0}, {1}",
                    reader[0], reader[1]));
            }
        }
    }
}
Public Sub ReadOrderData(ByVal connectionString As String)
    Dim queryString As String = _
        "SELECT OrderID, CustomerID FROM dbo.Orders;"
    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(queryString, connection)
        connection.Open()
        Dim reader As SqlDataReader = command.ExecuteReader()
        Try
            While reader.Read()
                Console.WriteLine(String.Format("{0}, {1}", _
                    reader(0), reader(1)))
            End While
        Finally
            ' Always call Close when done reading.
            reader.Close()
        End Try
    End Using
End Sub

注解

IDbCommand 接口允许继承类实现命令类,该命令类表示在数据源中执行的 SQL 语句。The IDbCommand interface enables an inheriting class to implement a Command class, which represents an SQL statement that is executed at a data source. 有关命令类的详细信息,请参阅执行命令For more information about Command classes, see Executing a Command.

应用程序不会直接创建 IDbCommand 接口的实例,而是创建实现 IDbCommand 接口的类的实例。An application does not create an instance of the IDbCommand interface directly, but creates an instance of a class that implements the IDbCommand interface.

实现 IDbCommand 的类必须实现其所有成员,并且通常定义其他成员来添加特定于提供程序的功能。Classes that implement IDbCommand must implement all its members, and typically define additional members to add provider-specific functionality. 例如,IDbCommand 接口定义 ExecuteNonQuery 方法。For example, the IDbCommand interface defines the ExecuteNonQuery method. 反过来,SqlCommand 类继承此方法,还定义 ExecuteXmlReader 方法。In turn, the SqlCommand class inherits this method, and also defines the ExecuteXmlReader method.

实施者说明

若要提高 .NET Framework 数据提供程序之间的一致性,请将继承类命名为形式 PrvClassname 其中 Prv 是为特定 .NET Framework 数据提供程序命名空间中的所有类指定的统一前缀。To promote consistency among .NET Framework data providers, name the inheriting class in the form PrvClassname where Prv is the uniform prefix given to all classes in a specific .NET Framework data provider namespace. 例如,SqlSystem.Data.SqlClient 命名空间中 SqlCommand 类的前缀。For example, Sql is the prefix of the SqlCommand class in the System.Data.SqlClient namespace.

当从 IDbCommand 接口继承时,应实现以下构造函数:When you inherit from the IDbCommand interface, you should implement the following constructors:

Item 说明Description
PrvCommand()PrvCommand() 初始化 PrvCommand 类的新实例。Initializes a new instance of the PrvCommand class.
PrvCommand (string cmdText)PrvCommand(string cmdText) 使用查询的文本初始化 PrvCommand 类的新实例。Initializes a new instance of the PrvCommand class with the text of the query.
PrvCommand (string cmdText,PrvConnection connection)PrvCommand(string cmdText, PrvConnection connection) 使用查询的文本和 PrvConnection 初始化 PrvCommand 类的新实例。Initializes a new instance of the PrvCommand class with the text of the query and a PrvConnection.
PrvCommand (string cmdText、PrvConnection connection、PrvTransaction transaction)PrvCommand(string cmdText, PrvConnection connection, PrvTransaction transaction) 使用查询文本、PrvConnection 和 PrvTransaction 初始化 PrvCommand 类的新实例。Initializes a new instance of the PrvCommand class with the text of the query, a PrvConnection, and the PrvTransaction.

属性

CommandText

获取或设置对数据源运行的文本命令。Gets or sets the text command to run against the data source.

CommandTimeout

获取或设置在终止尝试执行命令并生成错误之前的等待时间(以秒为单位)。Gets or sets the wait time (in seconds) before terminating the attempt to execute a command and generating an error.

CommandType

指示或指定解释 CommandText 属性的方式。Indicates or specifies how the CommandText property is interpreted.

Connection

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

Parameters

获取 IDataParameterCollectionGets the IDataParameterCollection.

Transaction

获取或设置要在其中执行 .NET Framework 数据提供程序的 Command 对象的事务。Gets or sets the transaction within which the Command object of a .NET Framework data provider executes.

UpdatedRowSource

获取或设置命令结果在由 DbDataAdapterUpdate(DataSet) 方法使用时应用于 DataRow 的方式。Gets or sets how command results are applied to the DataRow when used by the Update(DataSet) method of a DbDataAdapter.

方法

Cancel()

尝试取消执行 IDbCommandAttempts to cancels the execution of an IDbCommand.

CreateParameter()

创建 IDbDataParameter 对象的新实例。Creates a new instance of an IDbDataParameter object.

Dispose()

执行与释放或重置非托管资源关联的应用程序定义的任务。Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

(继承自 IDisposable)
ExecuteNonQuery()

对 .NET Framework 数据提供程序的 Connection 对象执行 SQL 语句,并返回受影响的行数。Executes an SQL statement against the Connection object of a .NET Framework data provider, and returns the number of rows affected.

ExecuteReader()

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

ExecuteReader(CommandBehavior)

Connection 执行 CommandText,并使用 CommandBehavior 值之一生成 IDataReaderExecutes the CommandText against the Connection, and builds an IDataReader using one of the CommandBehavior values.

ExecuteScalar()

执行查询,并返回由查询返回的结果集中第一行的第一列。Executes the query, and returns the first column of the first row in the resultset returned by the query. 忽略其他列或行。Extra columns or rows are ignored.

Prepare()

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

适用于

另请参阅