IDbCommand Interface

Represents an SQL statement or stored procedure to execute against a data source.

Namespace: System.Data

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

Properties
Methods
Examples

Properties

NameTypeDescription
CommandText string Gets or sets the text command to execute at the data source.
CommandTimeout int The wait time before terminating the attempt to execute a command and generating an error.
CommandType CommandType Specifies how the CommandText property is interpreted.
Connection IDbConnection The IDbConnection object that the command uses to communicate with the data source.
Parameters IDataParameterCollection Gets the collection of parameters for the command.
Transaction IDbTransaction The transaction within which the command executes.
UpdatedRowSource UpdateRowSource Specifies how command results are applied to a DataRow when used by the DataAdapter.

Methods

SignatureDescription
int ExecuteNonQuery() Executes a command that does not return any result set.
IDataReader ExecuteReader() Creates a IDataReader to read the result set generated by the command.
IDataReader ExecuteReader(CommandBehavior behavior) Creates an IDataReader with the specified behavior.
object ExecuteScalar() Executes the query and returns the first column of the first row in the result set.
void Prepare() Prepares the command for execution.
void Cancel() Cancels the execution of the command.

Example: Using IDbCommand with SQLite

// Install System.Data.SQLite via NuGet before running this example.
using System;
using System.Data;
using System.Data.SQLite;

class Program
{
    static void Main()
    {
        using (IDbConnection conn = new SQLiteConnection("Data Source=:memory:"))
        {
            conn.Open();

            // Create table
            using (IDbCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText = "CREATE TABLE Employees(Id INTEGER PRIMARY KEY, Name TEXT, Salary REAL);";
                cmd.ExecuteNonQuery();
            }

            // Insert data
            using (IDbCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText = "INSERT INTO Employees(Name, Salary) VALUES(@name, @salary);";

                var pName = cmd.CreateParameter();
                pName.ParameterName = "@name";
                pName.Value = "Alice";
                cmd.Parameters.Add(pName);

                var pSalary = cmd.CreateParameter();
                pSalary.ParameterName = "@salary";
                pSalary.Value = 75000;
                cmd.Parameters.Add(pSalary);

                cmd.ExecuteNonQuery();
            }

            // Query data
            using (IDbCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText = "SELECT Id, Name, Salary FROM Employees;";
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Console.WriteLine($"{reader.GetInt32(0)} - {reader.GetString(1)} - {reader.GetDecimal(2)}");
                    }
                }
            }
        }
    }
}

This example demonstrates how to create a table, insert a row, and read the data using the IDbCommand interface.