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 class NpgsqlCommand : DbCommand, IDbCommand, IAsyncDisposable, ICloneable, IComponent, IDisposable
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 result columns are requested 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 SQL statement or function (stored procedure) to execute. The default is an empty string.

CommandTimeout

Gets or sets the wait time (in seconds) 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 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 null.

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.

[Obsolete("Use the new DbBatch API")]
public IReadOnlyList<NpgsqlBatchCommand> Statements { get; }

Property Value

IReadOnlyList<NpgsqlBatchCommand>

Transaction

This property is ignored by Npgsql. PostgreSQL only supports a single transaction at a given time on a given connection, and all commands implicitly run inside the current transaction started via BeginTransaction()

public NpgsqlTransaction? Transaction { get; set; }

Property Value

NpgsqlTransaction

UnknownResultTypeList

Marks the query's result columns as known or unknown, on a column-by-column basis. Unknown result columns are requested 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 an 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 virtual NpgsqlCommand Clone()

Returns

NpgsqlCommand

A new NpgsqlCommand object.

CreateDbParameter()

Creates a new instance of an DbParameter object.

protected override DbParameter CreateDbParameter()

Returns

DbParameter

A DbParameter object.

CreateParameter()

Creates a new instance of a NpgsqlParameter object.

public NpgsqlParameter CreateParameter()

Returns

NpgsqlParameter

An NpgsqlParameter object.

Dispose(bool)

Releases the unmanaged resources used by the Component and optionally releases the managed resources.

protected override void Dispose(bool disposing)

Parameters

disposing bool

true to release both managed and unmanaged resources; false to release only unmanaged resources.

ExecuteDbDataReader(CommandBehavior)

Executes the command text against the connection.

protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)

Parameters

behavior CommandBehavior

Returns

DbDataReader

A task representing the operation.

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

An optional token to cancel the asynchronous operation. The default value is None.

Returns

Task<DbDataReader>

A task representing the asynchronous operation.

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

An optional token to cancel the asynchronous operation. The default value is None.

Returns

Task<int>

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

ExecuteReader(CommandBehavior)

Executes the CommandText against the Connection and returns a NpgsqlDataReader.

public NpgsqlDataReader ExecuteReader(CommandBehavior behavior = CommandBehavior.Default)

Parameters

behavior CommandBehavior

One of the enumeration values that specifies the command behavior.

Returns

NpgsqlDataReader

A task representing the operation.

ExecuteReaderAsync(CommandBehavior, CancellationToken)

An asynchronous version of ExecuteReader(CommandBehavior), which executes the CommandText against the Connection and returns a NpgsqlDataReader.

public Task<NpgsqlDataReader> ExecuteReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken = default)

Parameters

behavior CommandBehavior

One of the enumeration values that specifies the command behavior.

cancellationToken CancellationToken

An optional token to cancel the asynchronous operation. The default value is None.

Returns

Task<NpgsqlDataReader>

A task representing the asynchronous operation.

ExecuteReaderAsync(CancellationToken)

An asynchronous version of ExecuteReader(CommandBehavior), which executes the CommandText against the Connection and returns a NpgsqlDataReader.

public Task<NpgsqlDataReader> ExecuteReaderAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

An optional token to cancel the asynchronous operation. The default value is None.

Returns

Task<NpgsqlDataReader>

A task representing the asynchronous operation.

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

An optional token to cancel the asynchronous operation. The default value is None.

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(CancellationToken)

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

public override Task PrepareAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

An optional token to cancel the asynchronous operation. The default value is None.

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()

UnprepareAsync(CancellationToken)

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 Task UnprepareAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

An optional token to cancel the asynchronous operation. The default value is None.

Returns

Task