Table of Contents

Class NpgsqlDataReader

Namespace
Npgsql
Assembly
Npgsql.dll

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

public abstract class NpgsqlDataReader : DbDataReader, IDataReader, IDisposable, IDataRecord, IEnumerable, IAsyncDisposable
Inheritance
NpgsqlDataReader
Implements
Inherited Members

Fields

Behavior

The behavior of the command with which this reader was executed.

protected CommandBehavior Behavior

Field Value

CommandBehavior

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

Statements

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

public IReadOnlyList<NpgsqlStatement> Statements { get; }

Property Value

IReadOnlyList<NpgsqlStatement>

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

Returns

Task

Dispose(bool)

Releases the resources used by the NpgsqlDataReader.

protected override void Dispose(bool disposing)

Parameters

disposing bool

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>

GetDataTypeName(int)

Gets the data type information for the specified field. This will be 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.

GetDate(int)

Gets the value of the specified column as an NpgsqlDate, Npgsql's provider-specific type for dates.

public NpgsqlDate GetDate(int ordinal)

Parameters

ordinal int

The zero-based column ordinal.

Returns

NpgsqlDate

The value of the specified column.

Remarks

PostgreSQL's date type represents dates from 4713 BC to 5874897 AD, while .NET's DateTime only supports years from 1 to 1999. If you require years outside this range use this accessor. The standard GetProviderSpecificValue(int) method will also return this type, but has the disadvantage of boxing the value. See http://www.postgresql.org/docs/current/static/datatype-datetime.html

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.

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.

public override Type GetFieldType(int ordinal)

Parameters

ordinal int

The zero-based column ordinal.

Returns

Type

The data type of the specified column.

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.

GetInterval(int)

Gets the value of the specified column as an NpgsqlTimeSpan, Npgsql's provider-specific type for time spans.

public NpgsqlTimeSpan GetInterval(int ordinal)

Parameters

ordinal int

The zero-based column ordinal.

Returns

NpgsqlTimeSpan

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. If you require values from outside TimeSpan's range use this accessor. The standard ADO.NET GetProviderSpecificValue(int) method will also return this type, but has the disadvantage of boxing the value. See http://www.postgresql.org/docs/current/static/datatype-datetime.html

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

GetProviderSpecificFieldType(int)

Returns the provider-specific field type of the specified column.

public override Type GetProviderSpecificFieldType(int ordinal)

Parameters

ordinal int

The zero-based column ordinal.

Returns

Type

The Type object that describes the data type of the specified column.

GetProviderSpecificValues(object[])

Gets all provider-specific attribute columns in the collection for the current row.

public override int GetProviderSpecificValues(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.

GetSchemaTable()

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

public override DataTable GetSchemaTable()

Returns

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)

Retrieves data as a Stream.

public Task<Stream> GetStreamAsync(int ordinal)

Parameters

ordinal int

The zero-based column ordinal.

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)

Retrieves data as a TextReader.

public Task<TextReader> GetTextReaderAsync(int ordinal)

Parameters

ordinal int

The zero-based column ordinal.

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 http://www.postgresql.org/docs/current/static/datatype-datetime.html

GetTimeStamp(int)

Gets the value of the specified column as an NpgsqlDateTime, Npgsql's provider-specific type for date/time timestamps. Note that this type covers both PostgreSQL's "timestamp with time zone" and "timestamp without time zone" types, which differ only in how they are converted upon input/output.

public NpgsqlDateTime GetTimeStamp(int ordinal)

Parameters

ordinal int

The zero-based column ordinal.

Returns

NpgsqlDateTime

The value of the specified column.

Remarks

PostgreSQL's timestamp type represents dates from 4713 BC to 5874897 AD, while .NET's DateTime only supports years from 1 to 1999. If you require years outside this range use this accessor. The standard GetProviderSpecificValue(int) method will also return this type, but has the disadvantage of boxing the value. See http://www.postgresql.org/docs/current/static/datatype-datetime.html

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.

NextResult()

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

public override sealed bool NextResult()

Returns

bool

NextResultAsync(CancellationToken)

This is the asynchronous version of NextResult. The cancellationToken parameter is currently ignored.

public override Task<bool> NextResultAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

Currently ignored.

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() The cancellation token is currently ignored.

public override Task<bool> ReadAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

Ignored for now.

Returns

Task<bool>

A task representing the asynchronous operation.

Events

ReaderClosed

Is raised whenever Close() is called.

public event EventHandler ReaderClosed

Event Type

EventHandler