# Class NpgsqlDataReader

Namespace
  Npgsql

Assembly
  Npgsql.dll

Reads a forward-only stream of rows from a data source.

    public sealed class NpgsqlDataReader : DbDataReader, IEnumerable, IDataReader, IDataRecord, IDisposable, IAsyncDisposable, IDbColumnSchemaGenerator

Inheritance
  object
  MarshalByRefObject
  DbDataReader
  NpgsqlDataReader

Implements
  IEnumerable
  IDataReader
  IDataRecord
  IDisposable
  IAsyncDisposable
  IDbColumnSchemaGenerator

Inherited Members
  DbDataReader.GetFieldValueAsync<T>(int)
  DbDataReader.IsDBNullAsync(int)
  DbDataReader.NextResultAsync()
  DbDataReader.ReadAsync()
  DbDataReader.VisibleFieldCount
  MarshalByRefObject.GetLifetimeService()
  MarshalByRefObject.InitializeLifetimeService()
  object.Equals(object)
  object.Equals(object, object)
  object.GetHashCode()
  object.GetType()
  object.ReferenceEquals(object, object)
  object.ToString()

## Properties

### Depth

Gets a value indicating the depth of nesting for the current row. Always returns zero.

    public override int Depth { get; }

#### Property Value

int

### FieldCount

Gets the number of columns in the current row.

    public override int FieldCount { get; }

#### Property Value

int

### HasRows

Gets a value that indicates whether this DbDataReader contains one or more rows.

    public override bool HasRows { get; }

#### Property Value

bool

### IsClosed

Gets a value indicating whether the data reader is closed.

    public override bool IsClosed { get; }

#### Property Value

bool

### IsOnRow

Indicates whether the reader is currently positioned on a row, i.e. whether reading a column is possible. This property is different from HasRows in that HasRows will return true even if attempting to read a column will fail, e.g. before Read() has been called

    public bool IsOnRow { get; }

#### Property Value

bool

### this[int]

Gets the value of the specified column as an instance of object.

    public override object this[int ordinal] { get; }

#### Parameters

`ordinal` int

  The zero-based column ordinal.

#### Property Value

object

  The value of the specified column.

### this[string]

Gets the value of the specified column as an instance of object.

    public override object this[string name] { get; }

#### Parameters

`name` string

  The name of the column.

#### Property Value

object

  The value of the specified column.

### RecordsAffected

Gets the number of rows changed, inserted, or deleted by execution of the SQL statement.

    public override int RecordsAffected { get; }

#### Property Value

int

  The number of rows changed, inserted, or deleted. -1 for SELECT statements; 0 if no rows were affected or the statement failed.

### Rows

Gets the number of rows changed, inserted, or deleted by execution of the SQL statement.

    public ulong Rows { get; }

#### Property Value

ulong

  The number of rows changed, inserted, or deleted. 0 for SELECT statements, if no rows were affected or the statement failed.

### Statements

Returns details about each statement that this reader will or has executed.

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

#### Property Value

IReadOnlyList<NpgsqlBatchCommand>

#### Remarks

Note that some fields (i.e. rows and oid) are only populated as the reader traverses the result.

For commands with multiple queries, this exposes the number of rows affected on a statement-by-statement basis, unlike RecordsAffected which exposes an aggregation across all statements.

## Methods

### Close()

Closes the NpgsqlDataReader reader, allowing a new command to be executed.

    public override void Close()

### CloseAsync()

Closes the NpgsqlDataReader reader, allowing a new command to be executed.

    public override Task CloseAsync()

#### Returns

Task

### Dispose(bool)

Releases the resources used by the NpgsqlDataReader.

    protected override void Dispose(bool disposing)

#### Parameters

`disposing` bool

### DisposeAsync()

Releases the resources used by the NpgsqlDataReader.

    public override ValueTask DisposeAsync()

#### Returns

ValueTask

### GetBoolean(int)

Gets the value of the specified column as a Boolean.

    public override bool GetBoolean(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

#### Returns

bool

  The value of the specified column.

### GetByte(int)

Gets the value of the specified column as a byte.

    public override byte GetByte(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

#### Returns

byte

  The value of the specified column.

### GetBytes(int, long, byte[]?, int, int)

Reads a stream of bytes from the specified column, starting at location indicated by dataOffset, into the buffer, starting at the location indicated by bufferOffset.

    public override long GetBytes(int ordinal, long dataOffset, byte[]? buffer, int bufferOffset, int length)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

`dataOffset` long

  The index within the row from which to begin the read operation.

`buffer` byte[]

  The buffer into which to copy the data.

`bufferOffset` int

  The index with the buffer to which the data will be copied.

`length` int

  The maximum number of characters to read.

#### Returns

long

  The actual number of bytes read.

### GetChar(int)

Gets the value of the specified column as a single character.

    public override char GetChar(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

#### Returns

char

  The value of the specified column.

### GetChars(int, long, char[]?, int, int)

Reads a stream of characters from the specified column, starting at location indicated by dataOffset, into the buffer, starting at the location indicated by bufferOffset.

    public override long GetChars(int ordinal, long dataOffset, char[]? buffer, int bufferOffset, int length)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

`dataOffset` long

  The index within the row from which to begin the read operation.

`buffer` char[]

  The buffer into which to copy the data.

`bufferOffset` int

  The index with the buffer to which the data will be copied.

`length` int

  The maximum number of characters to read.

#### Returns

long

  The actual number of characters read.

### GetColumnSchema()

Returns schema information for the columns in the current resultset.

    public ReadOnlyCollection<NpgsqlDbColumn> GetColumnSchema()

#### Returns

ReadOnlyCollection<NpgsqlDbColumn>

### GetColumnSchemaAsync(CancellationToken)

Asynchronously returns schema information for the columns in the current resultset.

    public Task<ReadOnlyCollection<NpgsqlDbColumn>> GetColumnSchemaAsync(CancellationToken cancellationToken = default)

#### Parameters

`cancellationToken` CancellationToken

#### Returns

Task<ReadOnlyCollection<NpgsqlDbColumn>>

### GetData(int)

Returns a nested data reader for the requested column. The column type must be a record or a to Npgsql known composite type, or an array thereof. Currently only supported in non-sequential mode.

    public NpgsqlNestedDataReader GetData(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

#### Returns

NpgsqlNestedDataReader

  A data reader.

### GetDataTypeName(int)

Gets the data type information for the specified field. This is the PostgreSQL type name (e.g. double precision), not the .NET type (see GetFieldType(int) for that).

    public override string GetDataTypeName(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column index.

#### Returns

string

### GetDataTypeOID(int)

Gets the OID for the PostgreSQL type for the specified field, as it appears in the pg_type table.

    public uint GetDataTypeOID(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column index.

#### Returns

uint

#### Remarks

This is a PostgreSQL-internal value that should not be relied upon and should only be used for debugging purposes.

### GetDateTime(int)

Gets the value of the specified column as a DateTime object.

    public override DateTime GetDateTime(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

#### Returns

DateTime

  The value of the specified column.

### GetDbDataReader(int)

Returns a DbDataReader object for the requested column ordinal that can be overridden with a provider-specific implementation.

    protected override DbDataReader GetDbDataReader(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

#### Returns

DbDataReader

  A DbDataReader object.

### GetDecimal(int)

Gets the value of the specified column as a decimal object.

    public override decimal GetDecimal(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

#### Returns

decimal

  The value of the specified column.

### GetDouble(int)

Gets the value of the specified column as a double-precision floating point number.

    public override double GetDouble(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

#### Returns

double

  The value of the specified column.

### GetEnumerator()

Returns an IEnumerator that can be used to iterate through the rows in the data reader.

    public override IEnumerator GetEnumerator()

#### Returns

IEnumerator

  An IEnumerator that can be used to iterate through the rows in the data reader.

### GetFieldType(int)

Gets the data type of the specified column.

    [UnconditionalSuppressMessage("ILLink", "IL2093", Justification = "Members are only dynamically accessed by Npgsql via GetFieldType by GetSchema, and only in certain cases. Holding PublicFields and PublicProperties metadata on all our mapped types just for that case is the wrong tradeoff.")]
    public override Type GetFieldType(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

#### Returns

Type

  The data type of the specified column.

### GetFieldValueAsync<T>(int, CancellationToken)

Asynchronously gets the value of the specified column as a type.

    public override Task<T> GetFieldValueAsync<T>(int ordinal, CancellationToken cancellationToken)

#### Parameters

`ordinal` int

  The type of the value to be returned.

`cancellationToken` CancellationToken

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

#### Returns

Task<T>

#### Type Parameters

`T`

  The type of the value to be returned.

### GetFieldValue<T>(int)

Synchronously gets the value of the specified column as a type.

    public override T GetFieldValue<T>(int ordinal)

#### Parameters

`ordinal` int

  The column to be retrieved.

#### Returns

T

  The column to be retrieved.

#### Type Parameters

`T`

  Synchronously gets the value of the specified column as a type.

### GetFloat(int)

Gets the value of the specified column as a single-precision floating point number.

    public override float GetFloat(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

#### Returns

float

  The value of the specified column.

### GetGuid(int)

Gets the value of the specified column as a globally-unique identifier (GUID).

    public override Guid GetGuid(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

#### Returns

Guid

  The value of the specified column.

### GetInt16(int)

Gets the value of the specified column as a 16-bit signed integer.

    public override short GetInt16(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

#### Returns

short

  The value of the specified column.

### GetInt32(int)

Gets the value of the specified column as a 32-bit signed integer.

    public override int GetInt32(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

#### Returns

int

  The value of the specified column.

### GetInt64(int)

Gets the value of the specified column as a 64-bit signed integer.

    public override long GetInt64(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

#### Returns

long

  The value of the specified column.

### GetName(int)

Gets the name of the column, given the zero-based column ordinal.

    public override string GetName(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

#### Returns

string

  The name of the specified column.

### GetOrdinal(string)

Gets the column ordinal given the name of the column.

    public override int GetOrdinal(string name)

#### Parameters

`name` string

  The name of the column.

#### Returns

int

  The zero-based column ordinal.

### GetPostgresType(int)

Gets a representation of the PostgreSQL data type for the specified field. The returned representation can be used to access various information about the field.

    public PostgresType GetPostgresType(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column index.

#### Returns

PostgresType

### GetSchemaTable()

Returns a System.Data.DataTable that describes the column metadata of the DataReader.

    [UnconditionalSuppressMessage("Composite type mapping currently isn't trimming-safe, and warnings are generated at the MapComposite level.", "IL2026")]
    public override DataTable? GetSchemaTable()

#### Returns

DataTable

### GetSchemaTableAsync(CancellationToken)

Asynchronously returns a System.Data.DataTable that describes the column metadata of the DataReader.

    [UnconditionalSuppressMessage("Composite type mapping currently isn't trimming-safe, and warnings are generated at the MapComposite level.", "IL2026")]
    public override Task<DataTable?> GetSchemaTableAsync(CancellationToken cancellationToken = default)

#### Parameters

`cancellationToken` CancellationToken

#### Returns

Task<DataTable>

### GetStream(int)

Retrieves data as a Stream.

    public override Stream GetStream(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

#### Returns

Stream

  The returned object.

### GetStreamAsync(int, CancellationToken)

Retrieves data as a Stream.

    public Task<Stream> GetStreamAsync(int ordinal, CancellationToken cancellationToken = default)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

`cancellationToken` CancellationToken

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

#### Returns

Task<Stream>

  The returned object.

### GetString(int)

Gets the value of the specified column as an instance of string.

    public override string GetString(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

#### Returns

string

  The value of the specified column.

### GetTextReader(int)

Retrieves data as a TextReader.

    public override TextReader GetTextReader(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

#### Returns

TextReader

  The returned object.

### GetTextReaderAsync(int, CancellationToken)

Retrieves data as a TextReader.

    public Task<TextReader> GetTextReaderAsync(int ordinal, CancellationToken cancellationToken = default)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

`cancellationToken` CancellationToken

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

#### Returns

Task<TextReader>

  The returned object.

### GetTimeSpan(int)

Gets the value of the specified column as a TimeSpan,

    public TimeSpan GetTimeSpan(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

#### Returns

TimeSpan

  The value of the specified column.

#### Remarks

PostgreSQL's interval type has has a resolution of 1 microsecond and ranges from -178000000 to 178000000 years, while .NET's TimeSpan has a resolution of 100 nanoseconds and ranges from roughly -29247 to 29247 years. See https://www.postgresql.org/docs/current/static/datatype-datetime.html

### GetValue(int)

Gets the value of the specified column as an instance of object.

    public override object GetValue(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

#### Returns

object

  The value of the specified column.

### GetValues(object[])

Populates an array of objects with the column values of the current row.

    public override int GetValues(object[] values)

#### Parameters

`values` object[]

  An array of Object into which to copy the attribute columns.

#### Returns

int

  The number of instances of object in the array.

### IsDBNull(int)

Gets a value that indicates whether the column contains nonexistent or missing values.

    public override bool IsDBNull(int ordinal)

#### Parameters

`ordinal` int

  The zero-based column ordinal.

#### Returns

bool

  true if the specified column is equivalent to DBNull; otherwise false.

### IsDBNullAsync(int, CancellationToken)

An asynchronous version of IsDBNull(int), which gets a value that indicates whether the column contains non-existent or missing values. The `cancellationToken` parameter is currently ignored.

    public override Task<bool> IsDBNullAsync(int ordinal, CancellationToken cancellationToken)

#### Parameters

`ordinal` int

  The zero-based column to be retrieved.

`cancellationToken` CancellationToken

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

#### Returns

Task<bool>

  true if the specified column value is equivalent to DBNull otherwise false.

### NextResult()

Advances the reader to the next result when reading the results of a batch of statements.

    public override bool NextResult()

#### Returns

bool

### NextResultAsync(CancellationToken)

This is the asynchronous version of NextResult.

    public override Task<bool> NextResultAsync(CancellationToken cancellationToken)

#### Parameters

`cancellationToken` CancellationToken

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

#### Returns

Task<bool>

  A task representing the asynchronous operation.

### Read()

Advances the reader to the next record in a result set.

    public override bool Read()

#### Returns

bool

  true if there are more rows; otherwise false.

#### Remarks

The default position of a data reader is before the first record. Therefore, you must call Read to begin accessing data.

### ReadAsync(CancellationToken)

This is the asynchronous version of Read()

    public override Task<bool> ReadAsync(CancellationToken cancellationToken)

#### Parameters

`cancellationToken` CancellationToken

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

#### Returns

Task<bool>

  A task representing the asynchronous operation.

## Events

### ReaderClosed

Is raised whenever Close() is called.

    public event EventHandler? ReaderClosed

#### Event Type

EventHandler