Class NpgsqlDataReader
Reads a forward-only stream of rows from a data source.
Implements
Namespace: Npgsql
Assembly: Npgsql.dll
Syntax
public sealed class NpgsqlDataReader : DbDataReader, IDbColumnSchemaGenerator
Properties
| Improve this Doc View SourceDepth
Gets a value indicating the depth of nesting for the current row. Always returns zero.
Declaration
public override int Depth { get; }
Property Value
Type | Description |
---|---|
int |
FieldCount
Gets the number of columns in the current row.
Declaration
public override int FieldCount { get; }
Property Value
Type | Description |
---|---|
int |
HasRows
Gets a value that indicates whether this DbDataReader contains one or more rows.
Declaration
public override bool HasRows { get; }
Property Value
Type | Description |
---|---|
bool |
IsClosed
Gets a value indicating whether the data reader is closed.
Declaration
public override bool IsClosed { get; }
Property Value
Type | Description |
---|---|
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
Declaration
public bool IsOnRow { get; }
Property Value
Type | Description |
---|---|
bool |
this[int]
Gets the value of the specified column as an instance of
Declaration
public override object this[int ordinal] { get; }
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
Property Value
Type | Description |
---|---|
object | The value of the specified column. |
this[string]
Gets the value of the specified column as an instance of
Declaration
public override object this[string name] { get; }
Parameters
Type | Name | Description |
---|---|---|
string | name | The name of the column. |
Property Value
Type | Description |
---|---|
object | The value of the specified column. |
RecordsAffected
Gets the number of rows changed, inserted, or deleted by execution of the SQL statement.
Declaration
public override int RecordsAffected { get; }
Property Value
Type | Description |
---|---|
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.
Declaration
public ulong Rows { get; }
Property Value
Type | Description |
---|---|
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.
Declaration
public IReadOnlyList<NpgsqlBatchCommand> Statements { get; }
Property Value
Type | Description |
---|---|
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
| Improve this Doc View SourceClose()
Closes the NpgsqlDataReader reader, allowing a new command to be executed.
Declaration
public override void Close()
CloseAsync()
Closes the NpgsqlDataReader reader, allowing a new command to be executed.
Declaration
public override Task CloseAsync()
Returns
Type | Description |
---|---|
Task |
Dispose(bool)
Releases the resources used by the NpgsqlDataReader.
Declaration
protected override void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
bool | disposing |
DisposeAsync()
Releases the resources used by the NpgsqlDataReader.
Declaration
public override ValueTask DisposeAsync()
Returns
Type | Description |
---|---|
ValueTask |
GetBoolean(int)
Gets the value of the specified column as a Boolean.
Declaration
public override bool GetBoolean(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
Returns
Type | Description |
---|---|
bool | The value of the specified column. |
GetByte(int)
Gets the value of the specified column as a byte.
Declaration
public override byte GetByte(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
Returns
Type | Description |
---|---|
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.
Declaration
public override long GetBytes(int ordinal, long dataOffset, byte[]? buffer, int bufferOffset, int length)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
long | dataOffset | The index within the row from which to begin the read operation. |
byte[] | buffer | The buffer into which to copy the data. |
int | bufferOffset | The index with the buffer to which the data will be copied. |
int | length | The maximum number of characters to read. |
Returns
Type | Description |
---|---|
long | The actual number of bytes read. |
GetChar(int)
Gets the value of the specified column as a single character.
Declaration
public override char GetChar(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
Returns
Type | Description |
---|---|
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.
Declaration
public override long GetChars(int ordinal, long dataOffset, char[]? buffer, int bufferOffset, int length)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
long | dataOffset | The index within the row from which to begin the read operation. |
char[] | buffer | The buffer into which to copy the data. |
int | bufferOffset | The index with the buffer to which the data will be copied. |
int | length | The maximum number of characters to read. |
Returns
Type | Description |
---|---|
long | The actual number of characters read. |
GetColumnSchema()
Returns schema information for the columns in the current resultset.
Declaration
public ReadOnlyCollection<NpgsqlDbColumn> GetColumnSchema()
Returns
Type | Description |
---|---|
ReadOnlyCollection<><NpgsqlDbColumn> |
GetColumnSchemaAsync(CancellationToken)
Asynchronously returns schema information for the columns in the current resultset.
Declaration
public Task<ReadOnlyCollection<NpgsqlDbColumn>> GetColumnSchemaAsync(CancellationToken cancellationToken = null)
Parameters
Type | Name | Description |
---|---|---|
CancellationToken | cancellationToken |
Returns
Type | Description |
---|---|
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.
Declaration
public NpgsqlNestedDataReader GetData(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
Returns
Type | Description |
---|---|
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).
Declaration
public override string GetDataTypeName(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column index. |
Returns
Type | Description |
---|---|
string |
GetDataTypeOID(int)
Gets the OID for the PostgreSQL type for the specified field, as it appears in the pg_type table.
Declaration
public uint GetDataTypeOID(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column index. |
Returns
Type | Description |
---|---|
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
Declaration
public override DateTime GetDateTime(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
Returns
Type | Description |
---|---|
DateTime | The value of the specified column. |
GetDbDataReader(int)
Declaration
protected override DbDataReader GetDbDataReader(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal |
Returns
Type | Description |
---|---|
DbDataReader |
GetDecimal(int)
Gets the value of the specified column as a
Declaration
public override decimal GetDecimal(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
Returns
Type | Description |
---|---|
decimal | The value of the specified column. |
GetDouble(int)
Gets the value of the specified column as a double-precision floating point number.
Declaration
public override double GetDouble(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
Returns
Type | Description |
---|---|
double | The value of the specified column. |
GetEnumerator()
Returns an
Declaration
public override IEnumerator GetEnumerator()
Returns
Type | Description |
---|---|
IEnumerator | An |
GetFieldType(int)
Gets the data type of the specified column.
Declaration
public override Type GetFieldType(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
Returns
Type | Description |
---|---|
Type | The data type of the specified column. |
GetFieldValue<T>(int)
Synchronously gets the value of the specified column as a type.
Declaration
public override T GetFieldValue<T>(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The column to be retrieved. |
Returns
Type | Description |
---|---|
T | The column to be retrieved. |
Type Parameters
Name | Description |
---|---|
T | Synchronously gets the value of the specified column as a type. |
GetFieldValueAsync<T>(int, CancellationToken)
Asynchronously gets the value of the specified column as a type.
Declaration
public override Task<T> GetFieldValueAsync<T>(int ordinal, CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The type of the value to be returned. |
CancellationToken | cancellationToken | An optional token to cancel the asynchronous operation. The default value is |
Returns
Type | Description |
---|---|
Task<><T> |
Type Parameters
Name | Description |
---|---|
T | The type of the value to be returned. |
GetFloat(int)
Gets the value of the specified column as a single-precision floating point number.
Declaration
public override float GetFloat(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
Returns
Type | Description |
---|---|
float | The value of the specified column. |
GetGuid(int)
Gets the value of the specified column as a globally-unique identifier (GUID).
Declaration
public override Guid GetGuid(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
Returns
Type | Description |
---|---|
Guid | The value of the specified column. |
GetInt16(int)
Gets the value of the specified column as a 16-bit signed integer.
Declaration
public override short GetInt16(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
Returns
Type | Description |
---|---|
short | The value of the specified column. |
GetInt32(int)
Gets the value of the specified column as a 32-bit signed integer.
Declaration
public override int GetInt32(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
Returns
Type | Description |
---|---|
int | The value of the specified column. |
GetInt64(int)
Gets the value of the specified column as a 64-bit signed integer.
Declaration
public override long GetInt64(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
Returns
Type | Description |
---|---|
long | The value of the specified column. |
GetName(int)
Gets the name of the column, given the zero-based column ordinal.
Declaration
public override string GetName(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
Returns
Type | Description |
---|---|
string | The name of the specified column. |
GetOrdinal(string)
Gets the column ordinal given the name of the column.
Declaration
public override int GetOrdinal(string name)
Parameters
Type | Name | Description |
---|---|---|
string | name | The name of the column. |
Returns
Type | Description |
---|---|
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.
Declaration
public PostgresType GetPostgresType(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column index. |
Returns
Type | Description |
---|---|
PostgresType |
GetProviderSpecificFieldType(int)
Returns the provider-specific field type of the specified column.
Declaration
public override Type GetProviderSpecificFieldType(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
Returns
Type | Description |
---|---|
Type | The Type object that describes the data type of the specified column. |
GetProviderSpecificValue(int)
Gets the value of the specified column as an instance of
Declaration
public override object GetProviderSpecificValue(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
Returns
Type | Description |
---|---|
object | The value of the specified column. |
GetProviderSpecificValues(object[])
Gets all provider-specific attribute columns in the collection for the current row.
Declaration
public override int GetProviderSpecificValues(object[] values)
Parameters
Type | Name | Description |
---|---|---|
object[] | values | An array of Object into which to copy the attribute columns. |
Returns
Type | Description |
---|---|
int | The number of instances of |
GetSchemaTable()
Returns a System.Data.DataTable that describes the column metadata of the DataReader.
Declaration
public override DataTable? GetSchemaTable()
Returns
Type | Description |
---|---|
DataTable? |
GetSchemaTableAsync(CancellationToken)
Asynchronously returns a System.Data.DataTable that describes the column metadata of the DataReader.
Declaration
public Task<DataTable?> GetSchemaTableAsync(CancellationToken cancellationToken = null)
Parameters
Type | Name | Description |
---|---|---|
CancellationToken | cancellationToken |
Returns
Type | Description |
---|---|
Task<><DataTable?> |
GetStream(int)
Retrieves data as a
Declaration
public override Stream GetStream(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
Returns
Type | Description |
---|---|
Stream | The returned object. |
GetStreamAsync(int, CancellationToken)
Retrieves data as a
Declaration
public Task<Stream> GetStreamAsync(int ordinal, CancellationToken cancellationToken = null)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
CancellationToken | cancellationToken | An optional token to cancel the asynchronous operation. The default value is |
Returns
Type | Description |
---|---|
Task<><Stream> | The returned object. |
GetString(int)
Gets the value of the specified column as an instance of
Declaration
public override string GetString(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
Returns
Type | Description |
---|---|
string | The value of the specified column. |
GetTextReader(int)
Retrieves data as a
Declaration
public override TextReader GetTextReader(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
Returns
Type | Description |
---|---|
TextReader | The returned object. |
GetTextReaderAsync(int, CancellationToken)
Retrieves data as a
Declaration
public Task<TextReader> GetTextReaderAsync(int ordinal, CancellationToken cancellationToken = null)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
CancellationToken | cancellationToken | An optional token to cancel the asynchronous operation. The default value is |
Returns
Type | Description |
---|---|
Task<><TextReader> | The returned object. |
GetTimeSpan(int)
Gets the value of the specified column as a TimeSpan,
Declaration
public TimeSpan GetTimeSpan(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
Returns
Type | Description |
---|---|
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
Declaration
public override object GetValue(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
Returns
Type | Description |
---|---|
object | The value of the specified column. |
GetValues(object[])
Populates an array of objects with the column values of the current row.
Declaration
public override int GetValues(object[] values)
Parameters
Type | Name | Description |
---|---|---|
object[] | values | An array of Object into which to copy the attribute columns. |
Returns
Type | Description |
---|---|
int | The number of instances of |
IsDBNull(int)
Gets a value that indicates whether the column contains nonexistent or missing values.
Declaration
public override bool IsDBNull(int ordinal)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column ordinal. |
Returns
Type | Description |
---|---|
bool | true if the specified column is equivalent to |
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.
Declaration
public override Task<bool> IsDBNullAsync(int ordinal, CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
int | ordinal | The zero-based column to be retrieved. |
CancellationToken | cancellationToken | An optional token to cancel the asynchronous operation. The default value is |
Returns
Type | Description |
---|---|
Task<><bool> | true if the specified column value is equivalent to |
NextResult()
Advances the reader to the next result when reading the results of a batch of statements.
Declaration
public override bool NextResult()
Returns
Type | Description |
---|---|
bool |
NextResultAsync(CancellationToken)
This is the asynchronous version of NextResult.
Declaration
public override Task<bool> NextResultAsync(CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
CancellationToken | cancellationToken | An optional token to cancel the asynchronous operation. The default value is |
Returns
Type | Description |
---|---|
Task<><bool> | A task representing the asynchronous operation. |
Read()
Advances the reader to the next record in a result set.
Declaration
public override bool Read()
Returns
Type | Description |
---|---|
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()
Declaration
public override Task<bool> ReadAsync(CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
CancellationToken | cancellationToken | An optional token to cancel the asynchronous operation. The default value is |
Returns
Type | Description |
---|---|
Task<><bool> | A task representing the asynchronous operation. |
Events
| Improve this Doc View SourceReaderClosed
Is raised whenever Close() is called.
Declaration
public event EventHandler? ReaderClosed
Event Type
Type | Description |
---|---|
EventHandler? |