Share via

IDbCommand Interface


Represents an SQL statement that is executed while connected to a data source, and is implemented by .NET 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


The following example creates instances of the derived classes, SqlConnection, SqlCommand, and SqlDataReader. The example reads through the data, writing it to the console. Finally, 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(
        SqlCommand command = new SqlCommand(
            queryString, connection);
        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)
        Dim reader As SqlDataReader = command.ExecuteReader()
            While reader.Read()
                Console.WriteLine(String.Format("{0}, {1}", _
                    reader(0), reader(1)))
            End While
            ' Always call Close when done reading.
        End Try
    End Using
End Sub


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.

An application does not create an instance of the IDbCommand interface directly, but creates an instance of a class that implements the IDbCommand interface.

Classes that implement IDbCommand must implement all its members, and typically define additional members to add provider-specific functionality. For example, the IDbCommand interface defines the ExecuteNonQuery method. In turn, the SqlCommand class inherits this method, and also defines the ExecuteXmlReader method.

Notes to Implementers

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. For example, Sql is the prefix of the SqlCommand class in the System.Data.SqlClient namespace.

When you inherit from the IDbCommand interface, you should implement the following constructors:

Item Description
PrvCommand() Initializes a new instance of the PrvCommand class.
PrvCommand(string cmdText) Initializes a new instance of the PrvCommand class with the text of the query.
PrvCommand(string cmdText, PrvConnection connection) Initializes a new instance of the PrvCommand class with the text of the query and a PrvConnection.
PrvCommand(string cmdText, PrvConnection connection, PrvTransaction transaction) Initializes a new instance of the PrvCommand class with the text of the query, a PrvConnection, and the PrvTransaction.



Gets or sets the text command to run against the data source.


Gets or sets the wait time (in seconds) before terminating the attempt to execute a command and generating an error.


Indicates or specifies how the CommandText property is interpreted.


Gets or sets the IDbConnection used by this instance of the IDbCommand.


Gets the IDataParameterCollection.


Gets or sets the transaction within which the Command object of a .NET data provider executes.


Gets or sets how command results are applied to the DataRow when used by the Update(DataSet) method of a DbDataAdapter.



Attempts to cancels the execution of an IDbCommand.


Creates a new instance of an IDbDataParameter object.


Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

(Inherited from IDisposable)

Executes an SQL statement against the Connection object of a .NET data provider, and returns the number of rows affected.


Executes the CommandText against the Connection and builds an IDataReader.


Executes the CommandText against the Connection, and builds an IDataReader using one of the CommandBehavior values.


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.


Creates a prepared (or compiled) version of the command on the data source.

Applies to

See also