.NET Framework Class Library 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)

Syntax
Public Interface IDbCommand _
Inherits IDisposable
public interface IDbCommand : IDisposable
public interface class IDbCommand : IDisposable
type IDbCommand =
interface
interface IDisposable
end
The IDbCommand type exposes the following members.

Properties

Methods

Remarks
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 ImplementersTo 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. |

Examples
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.
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
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();
}
}
}

Version Information
.NET FrameworkSupported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0 .NET Framework Client ProfileSupported in: 4, 3.5 SP1

Platforms
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role not supported), 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.

See Also
|
Biblioteca de clases de .NET Framework IDbCommand (Interfaz) Representa una instrucción SQL que se ejecuta mientras se está conectado a un origen de datos y la implementan los proveedores de datos de .NET Framework que tienen acceso a bases de datos relacionales.
Espacio de nombres:
System.Data
Ensamblado:
System.Data (en System.Data.dll)

Sintaxis
Public Interface IDbCommand _
Inherits IDisposable
public interface IDbCommand : IDisposable
public interface class IDbCommand : IDisposable
type IDbCommand =
interface
interface IDisposable
end
El tipo IDbCommand expone los siguientes miembros.

Propiedades

Métodos

Comentarios
La interfaz IDbCommand permite que una clase heredada implemente una clase Command, que representa una instrucción SQL que se ejecuta en un origen de datos. Para obtener más información sobre las clases Command, vea Ejecutar un comando (ADO.NET). Una aplicación no crea una instancia de la interfaz IDbCommand directamente, sino que la crea de una clase que hereda de IDbCommand. Las clases que heredan IDbCommand deben implementar todos los miembros heredados y suelen definir miembros adicionales para agregar la funcionalidad específica de proveedor. Por ejemplo, la interfaz IDbCommand define el método ExecuteNonQuery. A su vez, la clase SqlCommand hereda este método y también define el método ExecuteXmlReader. Notas para los implementadoresPara potenciar la coherencia entre los proveedores de datos de .NET Framework, asigne a la clase heredada un nombre con el formato PrvClassname, donde Prv es el prefijo uniforme que se asigna a todas las clases de un espacio de nombres de proveedor de datos de .NET Framework específico. Por ejemplo, Sql es el prefijo de la clase SqlCommand en el espacio de nombres System.Data.SqlClient. Al heredar de la interfaz IDbCommand, se deben implementar los siguientes constructores: Elemento | Descripción |
|---|
PrvCommand() | Inicializa una nueva instancia de la clase PrvCommand. | PrvCommand(string cmdText) | Inicializa una nueva instancia de la clase PrvCommand con el texto de la consulta. | PrvCommand(string cmdText, PrvConnection connection) | Inicializa una nueva instancia de la clase PrvCommand con el texto de la consulta y una PrvConnection. | PrvCommand(string cmdText, PrvConnection connection, PrvTransaction transaction) | Inicializa una nueva instancia de la clase PrvCommand con el texto de la consulta, una PrvConnection y PrvTransaction. |

Ejemplos
En el ejemplo siguiente se crean instancias de las clases derivadas SqlConnection, SqlCommand y SqlDataReader. Posteriormente, se leen los datos y se escriben en la consola. Por último, el ejemplo cierra SqlDataReader y, a continuación, SqlConnection.
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
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();
}
}
}

Información de versión
.NET FrameworkCompatible con: 4, 3.5, 3.0, 2.0, 1.1, 1.0 .NET Framework Client ProfileCompatible con: 4, 3.5 SP1

Plataformas
Windows 7, Windows Vista SP1 o posterior, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (no se admite Server Core), Windows Server 2008 R2 (se admite Server Core con SP1 o posterior), Windows Server 2003 SP2
.NET Framework no admite todas las versiones de todas las plataformas. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.

Vea también
|