Export (0) Print
Expand All
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.

For a list of all members of this type, see IDbCommand Members.

System.IDisposable
   System.Data.IDbCommand

[Visual Basic]
Public Interface IDbCommand
   Inherits IDisposable
[C#]
public interface IDbCommand : IDisposable
[C++]
public __gc __interface IDbCommand : public IDisposable
[JScript]
public interface IDbCommand implements IDisposable

Classes that Implement IDbCommand

Class Description
OdbcCommand Represents an SQL statement or stored procedure to execute against a data source. This class cannot be inherited.
OleDbCommand Represents an SQL statement or stored procedure to execute against a data source.
OracleCommand Represents an SQL statement or stored procedure to execute against a database. This class cannot be inherited.
SqlCeCommand Represents an SQL statement to execute against a data source.
SqlCommand Represents a Transact-SQL statement or stored procedure to execute against a SQL Server database. This class cannot be inherited.

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. For more information about implementing .NET Framework data providers, see Implementing a .NET Framework Data Provider.

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.

Example

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Public Sub ReadMyData(myConnString As String)
    Dim mySelectQuery As String = "SELECT OrderID, Customer FROM Orders"
    Dim myConnection As New SqlConnection(myConnString)
    Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
    myConnection.Open()
    Dim myReader As SqlDataReader = myCommand.ExecuteReader()
    Try
        While myReader.Read()
            Console.WriteLine((myReader.GetInt32(0).ToString & ", " & myReader.GetString(1)))
        End While
    Finally
        ' always call Close when done reading.
        myReader.Close()
        ' always call Close when done reading.
        myConnection.Close()
    End Try
End Sub 'ReadMyData

[C#] 
public void ReadMyData(string myConnString) 
{
   string mySelectQuery = "SELECT OrderID, Customer FROM Orders";
   SqlConnection myConnection = new SqlConnection(myConnString);
   SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
   myConnection.Open();
   SqlDataReader myReader = myCommand.ExecuteReader();
   try 
   {
     while (myReader.Read()) 
     {
       Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
     }
    }
    finally 
    {
    // always call Close when done reading.
    myReader.Close();
    // always call Close when done reading.
    myConnection.Close();
    }
 }

[C++] 
public:
void ReadMyData(String* myConnString) 
{
   String* mySelectQuery = S"SELECT OrderID, Customer FROM Orders";
   SqlConnection* myConnection = new SqlConnection(myConnString);
   SqlCommand* myCommand = new SqlCommand(mySelectQuery,myConnection);
   myConnection->Open();
   SqlDataReader* myReader = myCommand->ExecuteReader();
   try 
   {
     while (myReader->Read()) 
     {
       Console::WriteLine(S"{0}, {1}", __box(myReader->GetInt32(0)), myReader->GetString(1));
     }
    }
    __finally 
    {
    // always call Close when done reading.
    myReader->Close();
    // always call Close when done reading.
    myConnection->Close();
    }
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Data

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Data (in System.Data.dll)

See Also

IDbCommand Members | System.Data Namespace | DbDataAdapter | IDbConnection | OleDbCommand | SqlCommand

Show:
© 2014 Microsoft. All rights reserved.