This topic has not yet been rated - Rate this topic

IDbCommand Interface

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.

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)
public interface IDbCommand : IDisposable

The IDbCommand type exposes the following members.

  Name Description
Public property Supported by the XNA Framework CommandText Gets or sets the text command to run against the data source.
Public property Supported by the XNA Framework CommandTimeout Gets or sets the wait time before terminating the attempt to execute a command and generating an error.
Public property Supported by the XNA Framework CommandType Indicates or specifies how the CommandText property is interpreted.
Public property Supported by the XNA Framework Connection Gets or sets the IDbConnection used by this instance of the IDbCommand.
Public property Supported by the XNA Framework Parameters Gets the IDataParameterCollection.
Public property Supported by the XNA Framework Transaction Gets or sets the transaction within which the Command object of a .NET Framework data provider executes.
Public property Supported by the XNA Framework UpdatedRowSource Gets or sets how command results are applied to the DataRow when used by the Update method of a DbDataAdapter.
Top
  Name Description
Public method Supported by the XNA Framework Cancel Attempts to cancels the execution of an IDbCommand.
Public method Supported by the XNA Framework CreateParameter Creates a new instance of an IDbDataParameter object.
Public method Supported by the XNA Framework Dispose Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. (Inherited from IDisposable.)
Public method Supported by the XNA Framework ExecuteNonQuery Executes an SQL statement against the Connection object of a .NET Framework data provider, and returns the number of rows affected.
Public method Supported by the XNA Framework ExecuteReader() Executes the CommandText against the Connection and builds an IDataReader.
Public method Supported by the XNA Framework ExecuteReader(CommandBehavior) Executes the CommandText against the Connection, and builds an IDataReader using one of the CommandBehavior values.
Public method Supported by the XNA Framework 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.
Public method Supported by the XNA Framework Prepare Creates a prepared (or compiled) version of the command on the data source.
Top

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 (ADO.NET).

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

Classes that inherit IDbCommand must implement all inherited 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.

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(
               connectionString))
    {
        SqlCommand command = new SqlCommand(
            queryString, connection);
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        try
        {
            while (reader.Read())
            {
                Console.WriteLine(String.Format("{0}, {1}",
                    reader[0], reader[1]));
            }
        }
        finally
        {
            // Always call Close when done reading.
            reader.Close();
        }
    }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ