Table of Contents

Method Command

Namespace
Open.Database.Extensions
Assembly
Open.Database.Extensions.Core.dll

Command(IDbConnection, string, CommandType)

Creates an ExpressiveCommand that executes on the given connection.

public static ExpressiveCommand Command(this IDbConnection connection, string command, CommandType type = CommandType.Text)

Parameters

connection IDbConnection

The connection to execute on. It is opened only if it is closed, closed again afterwards if the command opened it, and never disposed.

command string

The command text, or the stored procedure name when type is StoredProcedure.

type CommandType

The command type. The default is Text.

Returns

ExpressiveCommand

A new ExpressiveCommand to configure with parameters and then execute.

Examples

int rows = connection
    .Command("UPDATE Customers SET Region = @region WHERE Id = @id")
    .AddParam("region", "West")
    .AddParam("id", 42)
    .ExecuteNonQuery();

Remarks

The command is not validated against the database when it is created; problems with the SQL surface as provider exceptions when it is executed. A command created from a connection supports one execution at a time.

Exceptions

ArgumentNullException

connection or command is null.

ArgumentException

command is empty or white space.

See Also

Command(IDbTransaction, string, CommandType)

Creates an ExpressiveCommand that executes within the given transaction.

public static ExpressiveCommand Command(this IDbTransaction transaction, string command, CommandType type = CommandType.Text)

Parameters

transaction IDbTransaction

The transaction to execute in. The command runs on the transaction's Connection.

command string

The command text, or the stored procedure name when type is StoredProcedure.

type CommandType

The command type. The default is Text.

Returns

ExpressiveCommand

A new ExpressiveCommand to configure with parameters and then execute.

Exceptions

ArgumentNullException

transaction or command is null.

ArgumentException

command is empty or white space.

Command(DbConnection, string, CommandType)

Creates an ExpressiveDbCommand that executes on the given connection.

public static ExpressiveDbCommand Command(this DbConnection connection, string command, CommandType type = CommandType.Text)

Parameters

connection DbConnection

The connection to execute on. It is opened only if it is closed, closed again afterwards if the command opened it, and never disposed.

command string

The command text, or the stored procedure name when type is StoredProcedure.

type CommandType

The command type. The default is Text.

Returns

ExpressiveDbCommand

A new ExpressiveDbCommand to configure with parameters and then execute.

Exceptions

ArgumentNullException

connection or command is null.

ArgumentException

command is empty or white space.

Command(DbTransaction, string, CommandType)

Creates an ExpressiveDbCommand that executes within the given transaction.

public static ExpressiveDbCommand Command(this DbTransaction transaction, string command, CommandType type = CommandType.Text)

Parameters

transaction DbTransaction

The transaction to execute in. The command runs on the transaction's Connection.

command string

The command text, or the stored procedure name when type is StoredProcedure.

type CommandType

The command type. The default is Text.

Returns

ExpressiveDbCommand

A new ExpressiveDbCommand to configure with parameters and then execute.

Exceptions

ArgumentNullException

transaction or command is null.

ArgumentException

command is empty or white space.

Command(IDbConnectionFactory, string, CommandType)

Creates an ExpressiveCommand that creates a new connection from a connection factory for every execution.

public static ExpressiveCommand Command(this IDbConnectionFactory connectionSource, string command, CommandType type = CommandType.Text)

Parameters

connectionSource IDbConnectionFactory

The factory that creates the connections. Each execution creates a connection, opens it if needed, and disposes it when finished.

command string

The command text, or the stored procedure name when type is StoredProcedure.

type CommandType

The command type. The default is Text.

Returns

ExpressiveCommand

A new ExpressiveCommand to configure with parameters and then execute.

Exceptions

ArgumentNullException

connectionSource or command is null.

ArgumentException

command is empty or white space.

See Also

Command(IDbConnectionPool, string, CommandType)

Creates an ExpressiveCommand that takes its connections from a connection pool.

public static ExpressiveCommand Command(this IDbConnectionPool connectionSource, string command, CommandType type = CommandType.Text)

Parameters

connectionSource IDbConnectionPool

The pool to take connections from. Each execution takes one, opens it if needed, and gives it back when finished.

command string

The command text, or the stored procedure name when type is StoredProcedure.

type CommandType

The command type. The default is Text.

Returns

ExpressiveCommand

A new ExpressiveCommand to configure with parameters and then execute.

Exceptions

ArgumentNullException

connectionSource or command is null.

ArgumentException

command is empty or white space.

Command<TConnection>(IDbConnectionFactory<TConnection>, string, CommandType)

Creates an ExpressiveDbCommand that creates a new connection from a connection factory for every execution.

public static ExpressiveDbCommand Command<TConnection>(this IDbConnectionFactory<TConnection> connectionSource, string command, CommandType type = CommandType.Text) where TConnection : DbConnection

Parameters

connectionSource IDbConnectionFactory<TConnection>

The factory that creates the connections. Each execution creates a connection, opens it if needed, and disposes it when finished.

command string

The command text, or the stored procedure name when type is StoredProcedure.

type CommandType

The command type. The default is Text.

Returns

ExpressiveDbCommand

A new ExpressiveDbCommand to configure with parameters and then execute.

Type Parameters

TConnection

The connection type.

Exceptions

ArgumentNullException

connectionSource or command is null.

ArgumentException

command is empty or white space.

Command<TConnection>(IDbConnectionPool<TConnection>, string, CommandType)

Creates an ExpressiveDbCommand that takes its connections from a connection pool.

public static ExpressiveDbCommand Command<TConnection>(this IDbConnectionPool<TConnection> connectionSource, string command, CommandType type = CommandType.Text) where TConnection : DbConnection

Parameters

connectionSource IDbConnectionPool<TConnection>

The pool to take connections from. Each execution takes one, opens it if needed, and gives it back when finished.

command string

The command text, or the stored procedure name when type is StoredProcedure.

type CommandType

The command type. The default is Text.

Returns

ExpressiveDbCommand

A new ExpressiveDbCommand to configure with parameters and then execute.

Type Parameters

TConnection

The connection type.

Exceptions

ArgumentNullException

connectionSource or command is null.

ArgumentException

command is empty or white space.

Command(Func<IDbConnection>, string, CommandType)

Creates an ExpressiveCommand that invokes a function to create a new connection for every execution.

public static ExpressiveCommand Command(this Func<IDbConnection> connectionSource, string command, CommandType type = CommandType.Text)

Parameters

connectionSource Func<IDbConnection>

The function that creates the connections. Each execution invokes it, opens the connection if needed, and disposes it when finished.

command string

The command text, or the stored procedure name when type is StoredProcedure.

type CommandType

The command type. The default is Text.

Returns

ExpressiveCommand

A new ExpressiveCommand to configure with parameters and then execute.

Exceptions

ArgumentNullException

command is null.

ArgumentException

command is empty or white space.

Command<TConnection>(Func<TConnection>, string, CommandType)

Creates an ExpressiveDbCommand that invokes a function to create a new connection for every execution.

public static ExpressiveDbCommand Command<TConnection>(this Func<TConnection> connectionSource, string command, CommandType type = CommandType.Text) where TConnection : DbConnection

Parameters

connectionSource Func<TConnection>

The function that creates the connections. Each execution invokes it, opens the connection if needed, and disposes it when finished.

command string

The command text, or the stored procedure name when type is StoredProcedure.

type CommandType

The command type. The default is Text.

Returns

ExpressiveDbCommand

A new ExpressiveDbCommand to configure with parameters and then execute.

Type Parameters

TConnection

The connection type.

Examples

// A new connection is created, opened, and disposed for every execution.
int count = createConnection
    .Command("SELECT COUNT(*) FROM Customers")
    .ExecuteScalar<int>();

Exceptions

ArgumentNullException

command is null.

ArgumentException

command is empty or white space.