Search Results for

    Show / Hide Table of Contents

    Class NpgsqlCommand

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

    Inheritance
    Object
    MarshalByRefObject
    Component
    DbCommand
    NpgsqlCommand
    Implements
    IDbCommand
    IAsyncDisposable
    ICloneable
    IComponent
    IDisposable
    Inherited Members
    DbCommand.ExecuteNonQueryAsync()
    DbCommand.ExecuteReader()
    DbCommand.ExecuteReaderAsync()
    DbCommand.ExecuteReaderAsync(CommandBehavior)
    DbCommand.ExecuteScalarAsync()
    DbCommand.IDbCommand.CreateParameter()
    DbCommand.IDbCommand.ExecuteReader()
    DbCommand.IDbCommand.ExecuteReader(CommandBehavior)
    DbCommand.DisposeAsync()
    DbCommand.IDbCommand.Connection
    DbCommand.IDbCommand.Parameters
    DbCommand.IDbCommand.Transaction
    Component.Dispose()
    Component.GetService(Type)
    Component.ToString()
    Component.CanRaiseEvents
    Component.Container
    Component.DesignMode
    Component.Events
    Component.Site
    Component.Disposed
    MarshalByRefObject.GetLifetimeService()
    MarshalByRefObject.InitializeLifetimeService()
    MarshalByRefObject.MemberwiseClone(Boolean)
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Object.ReferenceEquals(Object, Object)
    Namespace: Npgsql
    Assembly: Npgsql.dll
    Syntax
    public class NpgsqlCommand : DbCommand, IDbCommand, IAsyncDisposable, ICloneable, IComponent, IDisposable

    Constructors

    | Improve this Doc View Source

    NpgsqlCommand()

    Initializes a new instance of the NpgsqlCommand class.

    Declaration
    public NpgsqlCommand()
    | Improve this Doc View Source

    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
    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.

    | Improve this Doc View Source

    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
    String cmdText

    The text of the query.

    NpgsqlConnection connection

    A NpgsqlConnection that represents the connection to a PostgreSQL server.

    | Improve this Doc View Source

    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
    String cmdText

    The text of the query.

    Properties

    | Improve this Doc View Source

    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.

    Declaration
    public bool AllResultTypesAreUnknown { get; set; }
    Property Value
    Type Description
    Boolean
    | Improve this Doc View Source

    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
    String

    The SQL statement or function (stored procedure) to execute. The default is an empty string.

    Overrides
    DbCommand.CommandText
    | Improve this Doc View Source

    CommandTimeout

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

    Declaration
    public override int CommandTimeout { get; set; }
    Property Value
    Type Description
    Int32

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

    Overrides
    DbCommand.CommandTimeout
    | Improve this Doc View Source

    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
    CommandType

    One of the CommandType values. The default is Text.

    Overrides
    DbCommand.CommandType
    | Improve this Doc View Source

    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 null.

    | Improve this Doc View Source

    DbConnection

    DB connection.

    Declaration
    protected override DbConnection DbConnection { get; set; }
    Property Value
    Type Description
    DbConnection
    Overrides
    DbCommand.DbConnection
    | Improve this Doc View Source

    DbParameterCollection

    DB parameter collection.

    Declaration
    protected override DbParameterCollection DbParameterCollection { get; }
    Property Value
    Type Description
    DbParameterCollection
    Overrides
    DbCommand.DbParameterCollection
    | Improve this Doc View Source

    DbTransaction

    DB transaction.

    Declaration
    protected override DbTransaction DbTransaction { get; set; }
    Property Value
    Type Description
    DbTransaction
    Overrides
    DbCommand.DbTransaction
    | Improve this Doc View Source

    DesignTimeVisible

    Design time visible.

    Declaration
    public override bool DesignTimeVisible { get; set; }
    Property Value
    Type Description
    Boolean
    Overrides
    DbCommand.DesignTimeVisible
    | Improve this Doc View Source

    IsPrepared

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

    Declaration
    public bool IsPrepared { get; }
    Property Value
    Type Description
    Boolean
    | Improve this Doc View Source

    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.

    | Improve this Doc View Source

    Statements

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

    Declaration
    [Obsolete("Use the new DbBatch API")]
    public IReadOnlyList<NpgsqlBatchCommand> Statements { get; }
    Property Value
    Type Description
    IReadOnlyList<NpgsqlBatchCommand>
    | Improve this Doc View Source

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

    Declaration
    public NpgsqlTransaction Transaction { get; set; }
    Property Value
    Type Description
    NpgsqlTransaction
    | Improve this Doc View Source

    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.

    Declaration
    public bool[] UnknownResultTypeList { get; set; }
    Property Value
    Type Description
    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.

    | Improve this Doc View Source

    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
    UpdateRowSource

    One of the UpdateRowSource values.

    Overrides
    DbCommand.UpdatedRowSource

    Methods

    | Improve this Doc View Source

    Cancel()

    Attempts to cancel the execution of an NpgsqlCommand.

    Declaration
    public override void Cancel()
    Overrides
    DbCommand.Cancel()
    Remarks

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

    | Improve this Doc View Source

    Clone()

    Create a new command based on this one.

    Declaration
    public virtual NpgsqlCommand Clone()
    Returns
    Type Description
    NpgsqlCommand

    A new NpgsqlCommand object.

    | Improve this Doc View Source

    CreateDbParameter()

    Creates a new instance of an DbParameter object.

    Declaration
    protected override DbParameter CreateDbParameter()
    Returns
    Type Description
    DbParameter

    A DbParameter object.

    Overrides
    DbCommand.CreateDbParameter()
    | Improve this Doc View Source

    CreateParameter()

    Creates a new instance of a NpgsqlParameter object.

    Declaration
    public NpgsqlParameter CreateParameter()
    Returns
    Type Description
    NpgsqlParameter

    An NpgsqlParameter object.

    | Improve this Doc View Source

    Dispose(Boolean)

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

    Declaration
    protected override void Dispose(bool disposing)
    Parameters
    Type Name Description
    Boolean disposing
    Overrides
    Component.Dispose(Boolean)
    | Improve this Doc View Source

    ExecuteDbDataReader(CommandBehavior)

    Executes the command text against the connection.

    Declaration
    protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
    Parameters
    Type Name Description
    CommandBehavior behavior
    Returns
    Type Description
    DbDataReader

    A task representing the operation.

    Overrides
    DbCommand.ExecuteDbDataReader(CommandBehavior)
    | Improve this Doc View Source

    ExecuteDbDataReaderAsync(CommandBehavior, CancellationToken)

    Executes the command text against the connection.

    Declaration
    protected override async Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
    Parameters
    Type Name Description
    CommandBehavior behavior

    An instance of CommandBehavior.

    CancellationToken cancellationToken

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

    Returns
    Type Description
    Task<DbDataReader>

    A task representing the asynchronous operation.

    Overrides
    DbCommand.ExecuteDbDataReaderAsync(CommandBehavior, CancellationToken)
    | Improve this Doc View Source

    ExecuteNonQuery()

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

    Declaration
    public override int ExecuteNonQuery()
    Returns
    Type Description
    Int32

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

    Overrides
    DbCommand.ExecuteNonQuery()
    | Improve this Doc View Source

    ExecuteNonQueryAsync(CancellationToken)

    Asynchronous version of ExecuteNonQuery()

    Declaration
    public override Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken)
    Parameters
    Type Name Description
    CancellationToken cancellationToken

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

    Returns
    Type Description
    Task<Int32>

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

    Overrides
    DbCommand.ExecuteNonQueryAsync(CancellationToken)
    | Improve this Doc View Source

    ExecuteReader(CommandBehavior)

    Executes the CommandText against the Connection and returns a NpgsqlDataReader.

    Declaration
    public NpgsqlDataReader ExecuteReader(CommandBehavior behavior = CommandBehavior.Default)
    Parameters
    Type Name Description
    CommandBehavior behavior

    One of the enumeration values that specifies the command behavior.

    Returns
    Type Description
    NpgsqlDataReader

    A task representing the operation.

    | Improve this Doc View Source

    ExecuteReaderAsync(CommandBehavior, CancellationToken)

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

    Declaration
    public Task<NpgsqlDataReader> ExecuteReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken = default(CancellationToken))
    Parameters
    Type Name Description
    CommandBehavior behavior

    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
    Type Description
    Task<NpgsqlDataReader>

    A task representing the asynchronous operation.

    | Improve this Doc View Source

    ExecuteReaderAsync(CancellationToken)

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

    Declaration
    public Task<NpgsqlDataReader> ExecuteReaderAsync(CancellationToken cancellationToken = default(CancellationToken))
    Parameters
    Type Name Description
    CancellationToken cancellationToken

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

    Returns
    Type Description
    Task<NpgsqlDataReader>

    A task representing the asynchronous operation.

    | Improve this Doc View Source

    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
    Object

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

    Overrides
    DbCommand.ExecuteScalar()
    | Improve this Doc View Source

    ExecuteScalarAsync(CancellationToken)

    Asynchronous version of ExecuteScalar()

    Declaration
    public override Task<object> ExecuteScalarAsync(CancellationToken cancellationToken)
    Parameters
    Type Name Description
    CancellationToken cancellationToken

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

    Returns
    Type Description
    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.

    Overrides
    DbCommand.ExecuteScalarAsync(CancellationToken)
    | Improve this Doc View Source

    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
    DbCommand.Prepare()
    | Improve this Doc View Source

    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 override Task PrepareAsync(CancellationToken cancellationToken = default(CancellationToken))
    Parameters
    Type Name Description
    CancellationToken cancellationToken

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

    Returns
    Type Description
    Task
    Overrides
    DbCommand.PrepareAsync(CancellationToken)
    | Improve this Doc View Source

    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()
    | Improve this Doc View Source

    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.

    Declaration
    public Task UnprepareAsync(CancellationToken cancellationToken = default(CancellationToken))
    Parameters
    Type Name Description
    CancellationToken cancellationToken

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

    Returns
    Type Description
    Task

    Explicit Interface Implementations

    | Improve this Doc View Source

    IComponent.Disposed

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

    Declaration
    event EventHandler IComponent.Disposed
    Returns
    Type Description
    EventHandler
    | Improve this Doc View Source

    ICloneable.Clone()

    Create a new command based on this one.

    Declaration
    object ICloneable.Clone()
    Returns
    Type Description
    Object

    A new NpgsqlCommand object.

    Implements

    System.Data.IDbCommand
    System.IAsyncDisposable
    System.ICloneable
    System.ComponentModel.IComponent
    System.IDisposable
    • Improve this Doc
    • View Source
    In This Article
    Back to top © Copyright 2022 The Npgsql Development Team