Class FixedWidthDataReader<TRecord>

Namespace
Wolfgang.Etl.FixedWidth
Assembly
Wolfgang.Etl.FixedWidth.dll

A forward-only, read-only IDataReader over a fixed-width text source, with the field layout taken from the [FixedWidthField] / [FixedWidthSkip] attributes on TRecord (#26). Serves each field's value directly from the parsed line — no TRecord instance is created per row — which is the optimal shape for SqlBulkCopy, Load(IDataReader), and other ADO.NET consumers that would otherwise discard a POCO per row.

public sealed class FixedWidthDataReader<TRecord> : IDataReader, IDataRecord, IDisposable where TRecord : notnull

Type Parameters

TRecord

The type whose FixedWidthFieldAttribute-decorated properties define the column layout. It is used only for its layout metadata; instances are never constructed.

Inheritance
FixedWidthDataReader<TRecord>
Implements
Inherited Members

Examples

using var reader = new FixedWidthDataReader<CustomerRecord>(fileStream);
using var bulkCopy = new SqlBulkCopy(connection) { DestinationTableName = "Customers" };
await bulkCopy.WriteToServerAsync(reader);

Constructors

FixedWidthDataReader(Stream, ILogger<FixedWidthDataReader<TRecord>>)

Initializes a new FixedWidthDataReader<TRecord> from a Stream using the default options, with diagnostic logging.

[Obsolete("Use the constructor that takes FixedWidthDataReaderOptions. This overload will be removed in a future release.")]
public FixedWidthDataReader(Stream stream, ILogger<FixedWidthDataReader<TRecord>> logger)

Parameters

stream Stream

The stream to use.

logger ILogger<FixedWidthDataReader<TRecord>>

The logger to use for diagnostic output.

Exceptions

ArgumentNullException

stream is null.

FixedWidthDataReader(Stream, FixedWidthDataReaderOptions?, ILogger<FixedWidthDataReader<TRecord>>?)

Initializes a new FixedWidthDataReader<TRecord> reading from a Stream via an internal 64 KB-buffered StreamReader. The caller retains ownership of the stream (it is not closed), but Dispose() must be called to release the internal reader. Pass Encoding to decode with a specific encoding (defaults to UTF8).

public FixedWidthDataReader(Stream stream, FixedWidthDataReaderOptions? options = null, ILogger<FixedWidthDataReader<TRecord>>? logger = null)

Parameters

stream Stream

The readable fixed-width stream.

options FixedWidthDataReaderOptions

Optional stream-decoding options. When null (the default), the documented defaults on FixedWidthDataReaderOptions apply.

logger ILogger<FixedWidthDataReader<TRecord>>

An optional ILogger<TCategoryName> for diagnostic output. Pass null (the default) to disable logging.

Exceptions

ArgumentNullException

stream is null.

FixedWidthDataReader(TextReader, ILogger<FixedWidthDataReader<TRecord>>?)

Initializes a new FixedWidthDataReader<TRecord> reading from a TextReader. The caller owns the reader's lifetime — it is not disposed.

public FixedWidthDataReader(TextReader reader, ILogger<FixedWidthDataReader<TRecord>>? logger = null)

Parameters

reader TextReader

The fixed-width text source.

logger ILogger<FixedWidthDataReader<TRecord>>

An optional ILogger<TCategoryName> for diagnostic output. Pass null (the default) to disable logging.

Exceptions

ArgumentNullException

reader is null.

Properties

BlankLineHandling

How blank lines are handled. Default ThrowException.

public BlankLineHandling BlankLineHandling { get; init; }

Property Value

BlankLineHandling

Depth

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

public int Depth { get; }

Property Value

int

The level of nesting.

Encoding

The Encoding used by this instance. Superseded by Encoding.

[Obsolete("Set Encoding on FixedWidthDataReaderOptions and pass it to the constructor instead. This property will be removed in a future release.")]
public Encoding Encoding { get; init; }

Property Value

Encoding

Remarks

Retained for source compatibility with 0.10.x. It is honoured only when the constructor was given no options object; a caller who passes options is using the supported route and that value wins. The two cannot conflict in existing code, because the options constructor did not exist before 0.11.0.

FieldCount

Gets the number of columns in the current row.

public int FieldCount { get; }

Property Value

int

When not positioned in a valid recordset, 0; otherwise, the number of columns in the current record. The default is -1.

FieldDelimiter

The inter-field delimiter used when the file was written, or null for none.

public string? FieldDelimiter { get; init; }

Property Value

string

HeaderLineCount

The number of leading lines to treat as a header and skip. Default 0.

public int HeaderLineCount { get; init; }

Property Value

int

IsClosed

Gets a value indicating whether the data reader is closed.

public bool IsClosed { get; }

Property Value

bool

true if the data reader is closed; otherwise, false.

this[int]

Gets the column located at the specified index.

public object this[int i] { get; }

Parameters

i int

The zero-based index of the column to get.

Property Value

object

The column located at the specified index as an object.

Exceptions

IndexOutOfRangeException

The index passed was outside the range of 0 through FieldCount.

this[string]

Gets the column with the specified name.

public object this[string name] { get; }

Parameters

name string

The name of the column to find.

Property Value

object

The column with the specified name as an object.

Exceptions

IndexOutOfRangeException

No column with the specified name was found.

MalformedLineHandling

How malformed lines are handled. Default ThrowException.

public MalformedLineHandling MalformedLineHandling { get; init; }

Property Value

MalformedLineHandling

MaximumItemCount

The maximum number of data rows to serve. Default MaxValue.

public long MaximumItemCount { get; init; }

Property Value

long

RecordsAffected

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

public int RecordsAffected { get; }

Property Value

int

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

SkipItemCount

The number of data rows to skip before the first row is served. Default 0.

public long SkipItemCount { get; init; }

Property Value

long

Methods

Close()

Closes the IDataReader Object.

public void Close()

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

GetBoolean(int)

Gets the value of the specified column as a Boolean.

public bool GetBoolean(int i)

Parameters

i int

The zero-based column ordinal.

Returns

bool

The value of the column.

Exceptions

IndexOutOfRangeException

The index passed was outside the range of 0 through FieldCount.

GetByte(int)

Gets the 8-bit unsigned integer value of the specified column.

public byte GetByte(int i)

Parameters

i int

The zero-based column ordinal.

Returns

byte

The 8-bit unsigned integer value of the specified column.

Exceptions

IndexOutOfRangeException

The index passed was outside the range of 0 through FieldCount.

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

Reads a stream of bytes from the specified column offset into the buffer as an array, starting at the given buffer offset.

public long GetBytes(int i, long fieldOffset, byte[]? buffer, int bufferoffset, int length)

Parameters

i int

The zero-based column ordinal.

fieldOffset long

The index within the field from which to start the read operation.

buffer byte[]

The buffer into which to read the stream of bytes.

bufferoffset int

The index for buffer to start the read operation.

length int

The number of bytes to read.

Returns

long

The actual number of bytes read.

Exceptions

IndexOutOfRangeException

The index passed was outside the range of 0 through FieldCount.

GetChar(int)

Gets the character value of the specified column.

public char GetChar(int i)

Parameters

i int

The zero-based column ordinal.

Returns

char

The character value of the specified column.

Exceptions

IndexOutOfRangeException

The index passed was outside the range of 0 through FieldCount.

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

Reads a stream of characters from the specified column offset into the buffer as an array, starting at the given buffer offset.

public long GetChars(int i, long fieldoffset, char[]? buffer, int bufferoffset, int length)

Parameters

i int

The zero-based column ordinal.

fieldoffset long

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

buffer char[]

The buffer into which to read the stream of bytes.

bufferoffset int

The index for buffer to start the read operation.

length int

The number of bytes to read.

Returns

long

The actual number of characters read.

Exceptions

IndexOutOfRangeException

The index passed was outside the range of 0 through FieldCount.

GetData(int)

Returns an IDataReader for the specified column ordinal.

public IDataReader GetData(int i)

Parameters

i int

The index of the field to find.

Returns

IDataReader

The IDataReader for the specified column ordinal.

Exceptions

IndexOutOfRangeException

The index passed was outside the range of 0 through FieldCount.

GetDataTypeName(int)

Gets the data type information for the specified field.

public string GetDataTypeName(int i)

Parameters

i int

The index of the field to find.

Returns

string

The data type information for the specified field.

Exceptions

IndexOutOfRangeException

The index passed was outside the range of 0 through FieldCount.

GetDateTime(int)

Gets the date and time data value of the specified field.

public DateTime GetDateTime(int i)

Parameters

i int

The index of the field to find.

Returns

DateTime

The date and time data value of the specified field.

Exceptions

IndexOutOfRangeException

The index passed was outside the range of 0 through FieldCount.

GetDecimal(int)

Gets the fixed-position numeric value of the specified field.

public decimal GetDecimal(int i)

Parameters

i int

The index of the field to find.

Returns

decimal

The fixed-position numeric value of the specified field.

Exceptions

IndexOutOfRangeException

The index passed was outside the range of 0 through FieldCount.

GetDouble(int)

Gets the double-precision floating point number of the specified field.

public double GetDouble(int i)

Parameters

i int

The index of the field to find.

Returns

double

The double-precision floating point number of the specified field.

Exceptions

IndexOutOfRangeException

The index passed was outside the range of 0 through FieldCount.

GetFieldType(int)

Gets the Type information corresponding to the type of object that would be returned from GetValue(int).

public Type GetFieldType(int i)

Parameters

i int

The index of the field to find.

Returns

Type

The Type information corresponding to the type of object that would be returned from GetValue(int).

Exceptions

IndexOutOfRangeException

The index passed was outside the range of 0 through FieldCount.

GetFloat(int)

Gets the single-precision floating point number of the specified field.

public float GetFloat(int i)

Parameters

i int

The index of the field to find.

Returns

float

The single-precision floating point number of the specified field.

Exceptions

IndexOutOfRangeException

The index passed was outside the range of 0 through FieldCount.

GetGuid(int)

Returns the GUID value of the specified field.

public Guid GetGuid(int i)

Parameters

i int

The index of the field to find.

Returns

Guid

The GUID value of the specified field.

Exceptions

IndexOutOfRangeException

The index passed was outside the range of 0 through FieldCount.

GetInt16(int)

Gets the 16-bit signed integer value of the specified field.

public short GetInt16(int i)

Parameters

i int

The index of the field to find.

Returns

short

The 16-bit signed integer value of the specified field.

Exceptions

IndexOutOfRangeException

The index passed was outside the range of 0 through FieldCount.

GetInt32(int)

Gets the 32-bit signed integer value of the specified field.

public int GetInt32(int i)

Parameters

i int

The index of the field to find.

Returns

int

The 32-bit signed integer value of the specified field.

Exceptions

IndexOutOfRangeException

The index passed was outside the range of 0 through FieldCount.

GetInt64(int)

Gets the 64-bit signed integer value of the specified field.

public long GetInt64(int i)

Parameters

i int

The index of the field to find.

Returns

long

The 64-bit signed integer value of the specified field.

Exceptions

IndexOutOfRangeException

The index passed was outside the range of 0 through FieldCount.

GetName(int)

Gets the name for the field to find.

public string GetName(int i)

Parameters

i int

The index of the field to find.

Returns

string

The name of the field or the empty string (""), if there is no value to return.

Exceptions

IndexOutOfRangeException

The index passed was outside the range of 0 through FieldCount.

GetOrdinal(string)

Return the index of the named field.

public int GetOrdinal(string name)

Parameters

name string

The name of the field to find.

Returns

int

The index of the named field.

GetSchemaTable()

Returns a DataTable that describes the column metadata of the IDataReader.

Returns null if the executed command returned no resultset, or after NextResult() returns false.

public DataTable GetSchemaTable()

Returns

DataTable

A DataTable that describes the column metadata.

Exceptions

InvalidOperationException

The IDataReader is closed.

GetString(int)

Gets the string value of the specified field.

public string GetString(int i)

Parameters

i int

The index of the field to find.

Returns

string

The string value of the specified field.

Exceptions

IndexOutOfRangeException

The index passed was outside the range of 0 through FieldCount.

GetValue(int)

Return the value of the specified field.

public object GetValue(int i)

Parameters

i int

The index of the field to find.

Returns

object

The object which will contain the field value upon return.

Exceptions

IndexOutOfRangeException

The index passed was outside the range of 0 through FieldCount.

GetValues(object[])

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

public int GetValues(object[] values)

Parameters

values object[]

An array of object to copy the attribute fields into.

Returns

int

The number of instances of object in the array.

IsDBNull(int)

Return whether the specified field is set to null.

public bool IsDBNull(int i)

Parameters

i int

The index of the field to find.

Returns

bool

true if the specified field is set to null; otherwise, false.

Exceptions

IndexOutOfRangeException

The index passed was outside the range of 0 through FieldCount.

NextResult()

Advances the data reader to the next result, when reading the results of batch SQL statements.

public bool NextResult()

Returns

bool

true if there are more rows; otherwise, false.

Read()

Advances the IDataReader to the next record.

public bool Read()

Returns

bool

true if there are more rows; otherwise, false.