Table of Contents

Class SheetDataReader

Namespace
ExcelReader.Core.Reader
Assembly
ExcelReader.Core.dll

Adapts one IExcelSheet to IDataReader, so it can feed SqlBulkCopy, DataTable.Load, Dapper, or any other ADO.NET consumer directly.

public sealed class SheetDataReader : IDataReader, IDataRecord, IDisposable
Inheritance
SheetDataReader
Implements
Inherited Members

Remarks

The column shape is fixed once, at construction time, from the header row (or from the first data row when HeaderRow is 0) — it never grows as later, wider rows come in. There is no schema pass, so GetFieldType(int), GetValue(int), and the typed getters all read the current row's own cell type; a consumer that builds a schema from the first Read() (e.g. DataTable.Load) locks in that row's types for the whole load.

One SheetDataReader reads one sheet: NextResult() always returns false. To read every sheet, construct one per SheetAt(int).

Disposing this reader disposes the row enumerator it created, not the workbook the sheet came from — the caller still owns that.

Constructors

SheetDataReader(IExcelSheet, SheetDataReaderOptions?)

Wraps sheet.

public SheetDataReader(IExcelSheet sheet, SheetDataReaderOptions? options = null)

Parameters

sheet IExcelSheet

The sheet to read. Its workbook stays owned by the caller.

options SheetDataReaderOptions

The header row; Default when null.

Exceptions

ArgumentNullException

sheet is null.

ArgumentOutOfRangeException

HeaderRow is negative.

InvalidOperationException

HeaderRow is positive and sheet is a table view (AsSheet()) of a table without a header row.

Properties

Depth

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

public int Depth { get; }

Property Value

int

The level of nesting.

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.

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.

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.

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.

[UnconditionalSuppressMessage("Trimming", "IL2111", Justification = "typeof(Type) as the 'DataType' schema column's own type is a metadata literal, never reflected over.")]
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)

Returns the value of the specified field.

public object GetValue(int i)

Parameters

i int

The index of the field to find.

Returns

object

The field value.

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

Always returns false — see the type-level remarks.

public bool NextResult()

Returns

bool

Read()

Advances the IDataReader to the next record.

public bool Read()

Returns

bool

true if there are more rows; otherwise, false.