Class ObjectDataReader<T>
- Namespace
- OdfKit.Spreadsheet
- Assembly
- OdfKit.dll
Adapts an IEnumerable<T> or IAsyncEnumerable<T> of arbitrary objects to a DbDataReader, mapping each readable public instance property of T to a column. This lets any object sequence (for example an Entity Framework Core query projection) flow through WriteDataAsync(DbDataReader, bool, CancellationToken) or any other DbDataReader consumer such as SqlBulkCopy, without OdfKit depending on any specific ORM or database provider.
將任意 IEnumerable<T> 或 IAsyncEnumerable<T> 物件序列轉接為 DbDataReader,把 T 的每個可讀公開執行個體屬性對應到一個資料行。這讓任意物件序列(例如 Entity Framework Core 查詢投影)可以直接餵給 WriteDataAsync(DbDataReader, bool, CancellationToken) 或其他 DbDataReader 消費者(如 SqlBulkCopy),且 OdfKit 本身不需相依任何特定 ORM 或資料庫 provider。
public sealed class ObjectDataReader<T> : DbDataReader, IDataReader, IDisposable, IDataRecord, IEnumerable, IAsyncDisposable
Type Parameters
TThe element type whose readable public instance properties become data reader columns. / 元素型別,其可讀公開執行個體屬性將成為資料讀取器的資料行。
- Inheritance
-
ObjectDataReader<T>
- Implements
- Inherited Members
Constructors
ObjectDataReader(IAsyncEnumerable<T>)
Initializes an ObjectDataReader<T> from an asynchronous element source, such as an Entity Framework Core IQueryable<T>.AsAsyncEnumerable() projection.
從非同步元素來源(例如 Entity Framework Core IQueryable<T>.AsAsyncEnumerable() 投影)初始化 ObjectDataReader<T>。
public ObjectDataReader(IAsyncEnumerable<T> source)
Parameters
sourceIAsyncEnumerable<T>The asynchronous element sequence to read. / 要讀取的非同步元素序列。
Exceptions
ObjectDataReader(IEnumerable<T>)
Initializes an ObjectDataReader<T> from a synchronous element source. 從同步元素來源初始化 ObjectDataReader<T>。
public ObjectDataReader(IEnumerable<T> source)
Parameters
sourceIEnumerable<T>The element sequence to read. / 要讀取的元素序列。
Exceptions
Properties
Depth
Gets the nesting depth of the current row. This implementation always returns 0. 取得目前資料列的巢狀深度。此實作恆傳回 0。
public override int Depth { get; }
Property Value
FieldCount
Gets the number of columns mapped from the readable public instance properties of T.
取得從 T 可讀公開執行個體屬性對應出的資料行數目。
public override int FieldCount { get; }
Property Value
HasRows
Gets a value indicating whether the data reader contains one or more rows. This implementation always returns true because the underlying sequence is not pre-enumerated. 取得一個值,指出資料讀取器是否包含一或多個資料列。因為底層序列不會預先列舉,此實作恆傳回 true。
public override bool HasRows { get; }
Property Value
IsClosed
Gets a value indicating whether the data reader is closed. 取得一個值,指出資料讀取器是否已關閉。
public override bool IsClosed { get; }
Property Value
this[int]
Gets the value of the column at the specified column index. 取得指定資料行索引處之資料行的值。
public override object this[int ordinal] { get; }
Parameters
ordinalintThe zero-based column index. / 採零起始的資料行索引。
Property Value
- object
The value of the column. / 該資料行的值。
this[string]
Gets the value of the column with the specified column name. 取得指定資料行名稱處之資料行的值。
public override object this[string name] { get; }
Parameters
namestringThe column name. / 資料行的名稱。
Property Value
- object
The value of the column. / 該資料行的值。
RecordsAffected
Gets the number of rows changed, inserted, or deleted by executing the statement. This implementation always returns -1. 取得執行陳述式所變更、插入或刪除的資料列數目。此實作恆傳回 -1。
public override int RecordsAffected { get; }
Property Value
Methods
Dispose(bool)
Releases unmanaged resources. 釋放非受控資源。
protected override void Dispose(bool disposing)
Parameters
disposingbool
GetBoolean(int)
Gets the value of the specified column as a Boolean. 取得指定資料行之布林值形式的值。
public override bool GetBoolean(int ordinal)
Parameters
ordinalintThe zero-based column index. / 採零起始的資料行索引。
Returns
- bool
The Boolean value of the column. / 該資料行的布林值。
GetByte(int)
Gets the value of the specified column as an 8-bit unsigned integer. 取得指定資料行之 8 位元無號整數值形式的值。
public override byte GetByte(int ordinal)
Parameters
ordinalintThe zero-based column index. / 採零起始的資料行索引。
Returns
- byte
The byte value of the column. / 該資料行的位元組值。
GetBytes(int, long, byte[]?, int, int)
Reads a stream of bytes from the specified column and copies it into the specified buffer. 從指定的資料行讀取位元組資料流,複製到指定的緩衝區。
public override long GetBytes(int ordinal, long dataOffset, byte[]? buffer, int bufferOffset, int length)
Parameters
ordinalintThe zero-based column index. / 採零起始的資料行索引。
dataOffsetlongThe index in the column where the read operation begins. / 資料行中開始讀取作業的索引。
bufferbyte[]The buffer into which data is copied. / 要將資料複製入其中的緩衝區。
bufferOffsetintThe index in the buffer where the write operation begins. / 緩衝區中開始寫入作業的索引。
lengthintThe maximum number of bytes to copy. / 要複製的最大位元組數。
Returns
- long
The actual number of bytes copied. / 實際複製的位元組數。
GetChar(int)
Gets the value of the specified column as a character. 取得指定資料行之字元形式的值。
public override char GetChar(int ordinal)
Parameters
ordinalintThe zero-based column index. / 採零起始的資料行索引。
Returns
- char
The character value of the column. / 該資料行的字元值。
GetChars(int, long, char[]?, int, int)
Reads a stream of characters from the specified column and copies it into the specified buffer. 從指定的資料行讀取字元資料流,複製到指定的緩衝區。
public override long GetChars(int ordinal, long dataOffset, char[]? buffer, int bufferOffset, int length)
Parameters
ordinalintThe zero-based column index. / 採零起始的資料行索引。
dataOffsetlongThe index in the column where the read operation begins. / 資料行中開始讀取作業的索引。
bufferchar[]The buffer into which data is copied. / 要將資料複製入其中的緩衝區。
bufferOffsetintThe index in the buffer where the write operation begins. / 緩衝區中開始寫入作業的索引。
lengthintThe maximum number of characters to copy. / 要複製的最大字元數。
Returns
- long
The actual number of characters copied. / 實際複製的字元數。
GetDataTypeName(int)
Gets the data type name of the specified column. 取得指定資料行的資料類型名稱。
public override string GetDataTypeName(int ordinal)
Parameters
ordinalintThe zero-based column index. / 採零起始的資料行索引。
Returns
- string
The data type name. / 資料類型的名稱。
GetDateTime(int)
public override DateTime GetDateTime(int ordinal)
Parameters
ordinalintThe zero-based column index. / 採零起始的資料行索引。
Returns
GetDecimal(int)
Gets the value of the specified column as a decimal number. 取得指定資料行之十進位數值形式的值。
public override decimal GetDecimal(int ordinal)
Parameters
ordinalintThe zero-based column index. / 採零起始的資料行索引。
Returns
- decimal
The decimal value of the column. / 該資料行的十進位數值。
GetDouble(int)
Gets the value of the specified column as a double-precision floating-point number. 取得指定資料行之倍精確度浮點數值形式的值。
public override double GetDouble(int ordinal)
Parameters
ordinalintThe zero-based column index. / 採零起始的資料行索引。
Returns
- double
The double-precision floating-point value of the column. / 該資料行的倍精確度浮點數值。
GetEnumerator()
Returns an enumerator that can be used to iterate through rows. 傳回可用來逐一查看資料列的列舉程式。
public override IEnumerator GetEnumerator()
Returns
- IEnumerator
An IEnumerator that can be used to iterate through results. / 一個 IEnumerator,可用於逐一查看結果。
GetFieldType(int)
Gets the Type of the specified column, taken from the mapped property type. 取得指定資料行的 Type(取自對應的屬性型別)。
public override Type GetFieldType(int ordinal)
Parameters
ordinalintThe zero-based column index. / 採零起始的資料行索引。
Returns
GetFloat(int)
Gets the value of the specified column as a single-precision floating-point number. 取得指定資料行之單精確度浮點數值形式的值。
public override float GetFloat(int ordinal)
Parameters
ordinalintThe zero-based column index. / 採零起始的資料行索引。
Returns
- float
The single-precision floating-point value of the column. / 該資料行的單精確度浮點數值。
GetGuid(int)
Gets the value of the specified column as a globally unique identifier (GUID). 取得指定資料行之全域唯一識別碼 (GUID) 形式的值。
public override Guid GetGuid(int ordinal)
Parameters
ordinalintThe zero-based column index. / 採零起始的資料行索引。
Returns
- Guid
The GUID value of the column. / 該資料行的 GUID 值。
GetInt16(int)
Gets the value of the specified column as a 16-bit signed integer. 取得指定資料行之 16 位元有號整數值形式的值。
public override short GetInt16(int ordinal)
Parameters
ordinalintThe zero-based column index. / 採零起始的資料行索引。
Returns
- short
The 16-bit signed integer value of the column. / 該資料行的 16 位元有號整數值。
GetInt32(int)
Gets the value of the specified column as a 32-bit signed integer. 取得指定資料行之 32 位元有號整數值形式的值。
public override int GetInt32(int ordinal)
Parameters
ordinalintThe zero-based column index. / 採零起始的資料行索引。
Returns
- int
The 32-bit signed integer value of the column. / 該資料行的 32 位元有號整數值。
GetInt64(int)
Gets the value of the specified column as a 64-bit signed integer. 取得指定資料行之 64 位元有號整數值形式的值。
public override long GetInt64(int ordinal)
Parameters
ordinalintThe zero-based column index. / 採零起始的資料行索引。
Returns
- long
The 64-bit signed integer value of the column. / 該資料行的 64 位元有號整數值。
GetName(int)
Gets the name of the specified column, taken from the mapped property name. 取得指定資料行的名稱(取自對應的屬性名稱)。
public override string GetName(int ordinal)
Parameters
ordinalintThe zero-based column index. / 採零起始的資料行索引。
Returns
- string
The column name. / 資料行的名稱。
GetOrdinal(string)
Gets the column index for the specified column name. 取得指定資料行名稱的資料行索引。
public override int GetOrdinal(string name)
Parameters
namestringThe column name. / 資料行的名稱。
Returns
- int
The zero-based column index, or -1 if the column is not found. / 採零起始的資料行索引;若找不到則為 -1。
GetSchemaTable()
Returns a DataTable that describes the reader metadata. This implementation always returns null. 傳回說明讀取器中繼資料的 DataTable。此實作恆傳回 null。
public override DataTable? GetSchemaTable()
Returns
- DataTable
A DataTable that describes this reader's schema metadata, or null. / 一個 DataTable,描述此讀取器的結構描述中繼資料;或是 null。
GetString(int)
Gets the value of the specified column as a string. 取得指定資料行之字串形式的值。
public override string GetString(int ordinal)
Parameters
ordinalintThe zero-based column index. / 採零起始的資料行索引。
Returns
- string
The string value of the column. / 該資料行的字串值。
GetValue(int)
Gets the raw value of the specified column in the current row. 取得目前資料列指定資料行的原始值。
public override object GetValue(int ordinal)
Parameters
ordinalintThe zero-based column index. / 採零起始的資料行索引。
Returns
- object
The value of the column. / 該資料行的值。
GetValues(object[])
Copies the values of all columns in the current row into the specified array. 將目前資料列中所有資料行的值複製到指定的陣列中。
public override int GetValues(object[] values)
Parameters
Returns
- int
The number of array items populated with values. / 陣列中被填入值的項目個數。
IsDBNull(int)
Gets a value indicating whether the specified column contains a null value. 取得一個值,指出指定資料行是否包含 Null 值。
public override bool IsDBNull(int ordinal)
Parameters
ordinalintThe zero-based column index. / 採零起始的資料行索引。
Returns
NextResult()
public override bool NextResult()
Returns
Read()
Advances to the next row of a synchronous source. Calling this on an instance constructed from an IAsyncEnumerable<T> synchronously blocks on the underlying asynchronous enumerator; callers with an asynchronous source should call ReadAsync(CancellationToken) instead. 讓同步來源前進至下一列。若對以 IAsyncEnumerable<T> 建構的執行個體呼叫此方法,會同步阻塞等待底層非同步列舉;非同步來源的呼叫端應改呼叫 ReadAsync(CancellationToken)。
public override bool Read()
Returns
ReadAsync(CancellationToken)
Asynchronously advances to the next row. 非同步讓資料讀取器前進至下一列。
public override Task<bool> ReadAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenThe cancellation token. / 取消權杖。