Class SheetDataReader
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
sheetIExcelSheetThe sheet to read. Its workbook stays owned by the caller.
optionsSheetDataReaderOptions
Exceptions
- ArgumentNullException
sheetis null.- ArgumentOutOfRangeException
HeaderRow is negative.
- InvalidOperationException
HeaderRow is positive and
sheetis 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
this[int]
Gets the column located at the specified index.
public object this[int i] { get; }
Parameters
iintThe zero-based index of the column to get.
Property Value
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
namestringThe name of the column to find.
Property Value
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
iintThe 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
iintThe 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
iintThe zero-based column ordinal.
fieldOffsetlongThe index within the field from which to start the read operation.
bufferbyte[]The buffer into which to read the stream of bytes.
bufferoffsetintThe index for
bufferto start the read operation.lengthintThe 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
iintThe 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
iintThe zero-based column ordinal.
fieldoffsetlongThe index within the row from which to start the read operation.
bufferchar[]The buffer into which to read the stream of bytes.
bufferoffsetintThe index for
bufferto start the read operation.lengthintThe 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
iintThe 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
iintThe 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
iintThe 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
iintThe 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
iintThe 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
iintThe 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
iintThe 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
iintThe 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
iintThe 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
iintThe 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
iintThe 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
iintThe 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
namestringThe 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
Exceptions
- InvalidOperationException
The IDataReader is closed.
GetString(int)
Gets the string value of the specified field.
public string GetString(int i)
Parameters
iintThe 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
iintThe 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
Returns
IsDBNull(int)
Return whether the specified field is set to null.
public bool IsDBNull(int i)
Parameters
iintThe index of the field to find.
Returns
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
Read()
Advances the IDataReader to the next record.
public bool Read()