Table of Contents

Class NpgsqlCommand

Namespace
Npgsql
Assembly
Npgsql.dll

Represents a SQL statement or function (stored procedure) to execute against a PostgreSQL database. This class cannot be inherited.

public sealed class NpgsqlCommand : DbCommand, IComponent, IDbCommand, IDisposable, IAsyncDisposable, ICloneable
Inheritance
NpgsqlCommand
Implements
Inherited Members

Constructors

NpgsqlCommand()

Initializes a new instance of the NpgsqlCommand class.

public NpgsqlCommand()

NpgsqlCommand(string)

Initializes a new instance of the NpgsqlCommand class with the text of the query.

public NpgsqlCommand(string cmdText)

Parameters

cmdText string

The text of the query.

NpgsqlCommand(string, NpgsqlConnection)

Initializes a new instance of the NpgsqlCommand class with the text of the query and a NpgsqlConnection.

public NpgsqlCommand(string cmdText, NpgsqlConnection connection)

Parameters

cmdText string

The text of the query.

connection NpgsqlConnection

A NpgsqlConnection that represents the connection to a PostgreSQL server.

NpgsqlCommand(string, NpgsqlConnection, NpgsqlTransaction)

Initializes a new instance of the NpgsqlCommand class with the text of the query, a NpgsqlConnection, and the NpgsqlTransaction.

public NpgsqlCommand(string cmdText, NpgsqlConnection connection, NpgsqlTransaction transaction)

Parameters

cmdText string

The text of the query.

connection NpgsqlConnection

A NpgsqlConnection that represents the connection to a PostgreSQL server.

transaction NpgsqlTransaction

The NpgsqlTransaction in which the NpgsqlCommand executes.

Properties

AllResultTypesAreUnknown

Marks all of the query's result columns as either known or unknown. Unknown results column are requested them from PostgreSQL in text format, and Npgsql makes no attempt to parse them. They will be accessible as strings only.

public bool AllResultTypesAreUnknown { get; set; }

Property Value

bool

CommandText

Gets or sets the SQL statement or function (stored procedure) to execute at the data source.

public override string CommandText { get; set; }

Property Value

string

The Transact-SQL statement or stored procedure to execute. The default is an empty string.

CommandTimeout

Gets or sets the wait time before terminating the attempt to execute a command and generating an error.

public override int CommandTimeout { get; set; }

Property Value

int

The time (in seconds) to wait for the command to execute. The default value is 30 seconds.

CommandType

Gets or sets a value indicating how the CommandText property is to be interpreted.

public override CommandType CommandType { get; set; }

Property Value

CommandType

One of the CommandType values. The default is CommandType.Text.

Connection

Gets or sets the NpgsqlConnection used by this instance of the NpgsqlCommand.

public NpgsqlConnection Connection { get; set; }

Property Value

NpgsqlConnection

The connection to a data source. The default value is a null reference.

DbConnection

DB connection.

protected override DbConnection DbConnection { get; set; }

Property Value

DbConnection

DbParameterCollection

DB parameter collection.

protected override DbParameterCollection DbParameterCollection { get; }

Property Value

DbParameterCollection

DbTransaction

DB transaction.

protected override DbTransaction DbTransaction { get; set; }

Property Value

DbTransaction

DesignTimeVisible

Design time visible.

public override bool DesignTimeVisible { get; set; }

Property Value

bool

IsPrepared

Returns whether this query will execute as a prepared (compiled) query.

public bool IsPrepared { get; }

Property Value

bool

Parameters

public NpgsqlParameterCollection Parameters { get; }

Property Value

NpgsqlParameterCollection

The parameters of the SQL statement or function (stored procedure). The default is an empty collection.

Statements

Returns details about each statement that this command has executed. Is only populated when an Execute* method is called.

public IReadOnlyList<NpgsqlStatement> Statements { get; }

Property Value

IReadOnlyList<NpgsqlStatement>

Transaction

Gets or sets the NpgsqlTransaction within which the NpgsqlCommand executes.

public NpgsqlTransaction Transaction { get; set; }

Property Value

NpgsqlTransaction

The NpgsqlTransaction. The default value is a null reference.

UnknownResultTypeList

Marks the query's result columns as known or unknown, on a column-by-column basis. Unknown results column are requested them from PostgreSQL in text format, and Npgsql makes no attempt to parse them. They will be accessible as strings only.

public bool[] UnknownResultTypeList { get; set; }

Property Value

bool[]

Remarks

If the query includes several queries (e.g. SELECT 1; SELECT 2), this will only apply to the first one. The rest of the queries will be fetched and parsed as usual.

The array size must correspond exactly to the number of result columns the query returns, or an error will be raised.

UpdatedRowSource

Gets or sets how command results are applied to the DataRow when used by the DbDataAdapter.Update(DataSet) method.

public override UpdateRowSource UpdatedRowSource { get; set; }

Property Value

UpdateRowSource

One of the UpdateRowSource values.

Methods

Cancel()

Attempts to cancel the execution of a NpgsqlCommand.

public override void Cancel()

Remarks

As per the specs, no exception will be thrown by this method in case of failure

Clone()

Create a new command based on this one.

public NpgsqlCommand Clone()

Returns

NpgsqlCommand

A new NpgsqlCommand object.

CreateDbParameter()

Creates a new instance of an DbParameter object.

protected override DbParameter CreateDbParameter()

Returns

DbParameter

An DbParameter object.

CreateParameter()

Creates a new instance of a NpgsqlParameter object.

public NpgsqlParameter CreateParameter()

Returns

NpgsqlParameter

A NpgsqlParameter object.

Dispose(bool)

Releases the resources used by the NpgsqlCommand.

protected override void Dispose(bool disposing)

Parameters

disposing bool

ExecuteDbDataReader(CommandBehavior)

Executes the command text against the connection.

protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)

Parameters

behavior CommandBehavior

Returns

DbDataReader

ExecuteDbDataReaderAsync(CommandBehavior, CancellationToken)

Executes the command text against the connection.

protected override Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)

Parameters

behavior CommandBehavior

An instance of CommandBehavior.

cancellationToken CancellationToken

A task representing the operation.

Returns

Task<DbDataReader>

ExecuteNonQuery()

Executes a SQL statement against the connection and returns the number of rows affected.

public override int ExecuteNonQuery()

Returns

int

The number of rows affected if known; -1 otherwise.

ExecuteNonQueryAsync(CancellationToken)

Asynchronous version of ExecuteNonQuery()

public override Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<int>

A task representing the asynchronous operation, with the number of rows affected if known; -1 otherwise.

ExecuteReader()

Executes the CommandText against the Connection, and returns an DbDataReader.

public NpgsqlDataReader ExecuteReader()

Returns

NpgsqlDataReader

A DbDataReader object.

Remarks

Unlike the ADO.NET method which it replaces, this method returns a Npgsql-specific DataReader.

ExecuteReader(CommandBehavior)

Executes the CommandText against the Connection, and returns an DbDataReader using one of the CommandBehavior values.

public NpgsqlDataReader ExecuteReader(CommandBehavior behavior)

Parameters

behavior CommandBehavior

Returns

NpgsqlDataReader

A DbDataReader object.

Remarks

Unlike the ADO.NET method which it replaces, this method returns a Npgsql-specific DataReader.

ExecuteScalar()

Executes the query, and returns the first column of the first row in the result set returned by the query. Extra columns or rows are ignored.

public override object ExecuteScalar()

Returns

object

The first column of the first row in the result set, or a null reference if the result set is empty.

ExecuteScalarAsync(CancellationToken)

Asynchronous version of ExecuteScalar()

public override Task<object> ExecuteScalarAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<object>

A task representing the asynchronous operation, with the first column of the first row in the result set, or a null reference if the result set is empty.

Prepare()

Creates a server-side prepared statement on the PostgreSQL server. This will make repeated future executions of this command much faster.

public override void Prepare()

PrepareAsync()

Creates a server-side prepared statement on the PostgreSQL server. This will make repeated future executions of this command much faster.

public Task PrepareAsync()

Returns

Task

PrepareAsync(CancellationToken)

Creates a server-side prepared statement on the PostgreSQL server. This will make repeated future executions of this command much faster.

public Task PrepareAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

Returns

Task

Unprepare()

Unprepares a command, closing server-side statements associated with it. Note that this only affects commands explicitly prepared with Prepare(), not automatically prepared statements.

public void Unprepare()