Class NpgsqlCommand
Represents a SQL statement or function (stored procedure) to execute against a PostgreSQL database. This class cannot be inherited.
Inheritance
Implements
Inherited Members
Namespace: Npgsql
Assembly: Npgsql.dll
Syntax
public sealed class NpgsqlCommand : DbCommand, IComponent, IDbCommand, IDisposable, ICloneable
Constructors
NpgsqlCommand()
Initializes a new instance of the NpgsqlCommand class.
Declaration
public NpgsqlCommand()
NpgsqlCommand(String)
Initializes a new instance of the NpgsqlCommand class with the text of the query.
Declaration
public NpgsqlCommand(string cmdText)
Parameters
Type | Name | Description |
---|---|---|
System.String | cmdText | 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.
Declaration
public NpgsqlCommand(string cmdText, NpgsqlConnection connection)
Parameters
Type | Name | Description |
---|---|---|
System.String | cmdText | The text of the query. |
NpgsqlConnection | connection | 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.
Declaration
public NpgsqlCommand(string cmdText, NpgsqlConnection connection, NpgsqlTransaction transaction)
Parameters
Type | Name | Description |
---|---|---|
System.String | cmdText | The text of the query. |
NpgsqlConnection | connection | A NpgsqlConnection that represents the connection to a PostgreSQL server. |
NpgsqlTransaction | transaction | 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.
Declaration
public bool AllResultTypesAreUnknown { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean |
CommandText
Gets or sets the SQL statement or function (stored procedure) to execute at the data source.
Declaration
public override string CommandText { get; set; }
Property Value
Type | Description |
---|---|
System.String | The Transact-SQL statement or stored procedure to execute. The default is an empty string. |
Overrides
CommandTimeout
Gets or sets the wait time before terminating the attempt to execute a command and generating an error.
Declaration
public override int CommandTimeout { get; set; }
Property Value
Type | Description |
---|---|
System.Int32 | The time (in seconds) to wait for the command to execute. The default value is 30 seconds. |
Overrides
CommandType
Gets or sets a value indicating how the CommandText property is to be interpreted.
Declaration
public override CommandType CommandType { get; set; }
Property Value
Type | Description |
---|---|
System.Data.CommandType | One of the System.Data.CommandType values. The default is System.Data.CommandType. |
Overrides
Connection
Gets or sets the NpgsqlConnection used by this instance of the NpgsqlCommand.
Declaration
public NpgsqlConnection Connection { get; set; }
Property Value
Type | Description |
---|---|
NpgsqlConnection | The connection to a data source. The default value is a null reference. |
DbConnection
DB connection.
Declaration
protected override DbConnection DbConnection { get; set; }
Property Value
Type | Description |
---|---|
System.Data.Common.DbConnection |
Overrides
DbParameterCollection
DB parameter collection.
Declaration
protected override DbParameterCollection DbParameterCollection { get; }
Property Value
Type | Description |
---|---|
System.Data.Common.DbParameterCollection |
Overrides
DbTransaction
DB transaction.
Declaration
protected override DbTransaction DbTransaction { get; set; }
Property Value
Type | Description |
---|---|
System.Data.Common.DbTransaction |
Overrides
DesignTimeVisible
Design time visible.
Declaration
public override bool DesignTimeVisible { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean |
Overrides
IsPrepared
Returns whether this query will execute as a prepared (compiled) query.
Declaration
public bool IsPrepared { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
Parameters
Gets the NpgsqlParameterCollection.
Declaration
public NpgsqlParameterCollection Parameters { get; }
Property Value
Type | Description |
---|---|
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.
Declaration
public IReadOnlyList<NpgsqlStatement> Statements { get; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.IReadOnlyList<NpgsqlStatement> |
Transaction
Gets or sets the NpgsqlTransaction within which the NpgsqlCommand executes.
Declaration
public NpgsqlTransaction Transaction { get; set; }
Property Value
Type | Description |
---|---|
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.
Declaration
public bool[] UnknownResultTypeList { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean[] |
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.
Declaration
public override UpdateRowSource UpdatedRowSource { get; set; }
Property Value
Type | Description |
---|---|
System.Data.UpdateRowSource | One of the System.Data.UpdateRowSource values. |
Overrides
Methods
Cancel()
Attempts to cancel the execution of a NpgsqlCommand.
Declaration
public override void Cancel()
Overrides
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.
Declaration
public NpgsqlCommand Clone()
Returns
Type | Description |
---|---|
NpgsqlCommand | A new NpgsqlCommand object. |
CreateDbParameter()
Creates a new instance of an System.Data.Common.DbParameter object.
Declaration
protected override DbParameter CreateDbParameter()
Returns
Type | Description |
---|---|
System.Data.Common.DbParameter | An System.Data.Common.DbParameter object. |
Overrides
CreateParameter()
Creates a new instance of a NpgsqlParameter object.
Declaration
public NpgsqlParameter CreateParameter()
Returns
Type | Description |
---|---|
NpgsqlParameter | A NpgsqlParameter object. |
Dispose(Boolean)
Releases the resources used by the NpgsqlCommand.
Declaration
protected override void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | disposing |
Overrides
ExecuteDbDataReader(CommandBehavior)
Executes the command text against the connection.
Declaration
protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
Parameters
Type | Name | Description |
---|---|---|
System.Data.CommandBehavior | behavior |
Returns
Type | Description |
---|---|
System.Data.Common.DbDataReader |
Overrides
ExecuteDbDataReaderAsync(CommandBehavior, CancellationToken)
Executes the command text against the connection.
Declaration
protected override Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
System.Data.CommandBehavior | behavior | An instance of System.Data.CommandBehavior. |
System.Threading.CancellationToken | cancellationToken | A task representing the operation. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Data.Common.DbDataReader> |
Overrides
ExecuteNonQuery()
Executes a SQL statement against the connection and returns the number of rows affected.
Declaration
public override int ExecuteNonQuery()
Returns
Type | Description |
---|---|
System.Int32 | The number of rows affected if known; -1 otherwise. |
Overrides
ExecuteNonQueryAsync(CancellationToken)
Asynchronous version of ExecuteNonQuery()
Declaration
public override Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
System.Threading.CancellationToken | cancellationToken | The token to monitor for cancellation requests. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Int32> | A task representing the asynchronous operation, with the number of rows affected if known; -1 otherwise. |
Overrides
ExecuteReader()
Executes the CommandText against the Connection, and returns an DbDataReader.
Declaration
public NpgsqlDataReader ExecuteReader()
Returns
Type | Description |
---|---|
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.
Declaration
public NpgsqlDataReader ExecuteReader(CommandBehavior behavior)
Parameters
Type | Name | Description |
---|---|---|
System.Data.CommandBehavior | behavior |
Returns
Type | Description |
---|---|
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.
Declaration
public override object ExecuteScalar()
Returns
Type | Description |
---|---|
System.Object | The first column of the first row in the result set, or a null reference if the result set is empty. |
Overrides
ExecuteScalarAsync(CancellationToken)
Asynchronous version of ExecuteScalar()
Declaration
public override Task<object> ExecuteScalarAsync(CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
System.Threading.CancellationToken | cancellationToken | The token to monitor for cancellation requests. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.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. |
Overrides
Prepare()
Creates a server-side prepared statement on the PostgreSQL server. This will make repeated future executions of this command much faster.
Declaration
public override void Prepare()
Overrides
PrepareAsync()
Creates a server-side prepared statement on the PostgreSQL server. This will make repeated future executions of this command much faster.
Declaration
public Task PrepareAsync()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task |
PrepareAsync(CancellationToken)
Creates a server-side prepared statement on the PostgreSQL server. This will make repeated future executions of this command much faster.
Declaration
public Task PrepareAsync(CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
System.Threading.CancellationToken | cancellationToken |
Returns
Type | Description |
---|---|
System.Threading.Tasks.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.
Declaration
public void Unprepare()
Explicit Interface Implementations
ICloneable.Clone()
Create a new command based on this one.
Declaration
object ICloneable.Clone()
Returns
Type | Description |
---|---|
System.Object | A new NpgsqlCommand object. |