Class CsvDataReader
Represents a reader that provides fast, non-cached, forward-only access to CSV data.
Inherited Members
Namespace: IRM.Data
Assembly: IRM.dll
Syntax
public class CsvDataReader : BaseDataReader, IDataReader, IDisposable, IDataRecord, IEnumerable<string[]>, IEnumerable
Constructors
CsvDataReader(TextReader, IDictionary<Int32, CsvColumn>, CsvReaderOptions)
Initializes a new instance of the CsvTypedReader class.
Declaration
public CsvDataReader(TextReader reader, IDictionary<int, CsvColumn> columns, CsvReaderOptions options)
Parameters
System.IO.TextReader
reader
A System.IO.TextReader pointing to the CSV file. |
System.Collections.Generic.IDictionary<System.Int32, CsvColumn>
columns
A Dictionary that describes thes columns in the CSV file |
CsvReaderOptions
options
A CsvReaderOptions object defining how to handle reading the csv file. |
Exceptions
System.ArgumentNullException
|
System.ArgumentException
Cannot read from |
CsvDataReader(TextReader, IDictionary<Int32, CsvColumn>, Int32, CsvReaderOptions)
Initializes a new instance of the CsvTypedReader class.
Declaration
public CsvDataReader(TextReader reader, IDictionary<int, CsvColumn> columns, int bufferSize, CsvReaderOptions options)
Parameters
System.IO.TextReader
reader
A System.IO.TextReader pointing to the CSV file. |
System.Collections.Generic.IDictionary<System.Int32, CsvColumn>
columns
A Dictionary that describes thes columns in the CSV file |
System.Int32
bufferSize
The buffer size in bytes. |
CsvReaderOptions
options
A CsvReaderOptions object defining how to handle reading the csv file. |
Exceptions
System.ArgumentNullException
|
ArgumentOutOfRangeException
|
CsvDataReader(TextReader, Type, CsvReaderOptions)
Initializes a new instance of the CsvDataReader class.
Declaration
public CsvDataReader(TextReader reader, Type type, CsvReaderOptions options)
Parameters
System.IO.TextReader
reader
A System.IO.TextReader pointing to the CSV file. |
System.Type
type
The type that corresponds to the columns in the CSV file. |
CsvReaderOptions
options
A CsvReaderOptions object defining how to handle reading the csv file. |
Exceptions
System.ArgumentNullException
|
System.ArgumentException
Cannot read from |
CsvDataReader(TextReader, Type, Boolean, CsvReaderOptions)
Initializes a new instance of the CsvDataReader class.
Declaration
public CsvDataReader(TextReader reader, Type type, bool includeInternalProperies, CsvReaderOptions options)
Parameters
System.IO.TextReader
reader
A System.IO.TextReader pointing to the CSV file. |
System.Type
type
The type that corresponds to the columns in the CSV file. |
Boolean
includeInternalProperies
True om internal properties från |
CsvReaderOptions
options
A CsvReaderOptions object defining how to handle reading the csv file. |
Exceptions
System.ArgumentNullException
|
System.ArgumentException
Cannot read from |
Fields
DefaultBufferSize
Defines the default buffer size.
Declaration
public const int DefaultBufferSize = 4096
Field Value
System.Int32
|
Properties
BufferSize
Gets the buffer size.
Declaration
public int BufferSize { get; }
Property Value
System.Int32
|
CurrentRecordIndex
Gets the current record index in the CSV file.
Declaration
public virtual long CurrentRecordIndex { get; }
Property Value
System.Int64
The current record index in the CSV file. |
EndOfStream
Gets a value that indicates whether the current stream position is at the end of the stream.
Declaration
public virtual bool EndOfStream { get; }
Property Value
Boolean
true if the current stream position is at the end of the stream; otherwise false. |
FieldCount
Gets the maximum number of fields to retrieve for each record.
Declaration
public override int FieldCount { get; }
Property Value
System.Int32
The maximum number of fields to retrieve for each record. |
Overrides
Exceptions
System.ObjectDisposedException
The instance has been disposed of. |
IsClosed
Gets a value indicating whether the data reader is closed.
Declaration
public override bool IsClosed { get; }
Property Value
Boolean
true if the data reader is closed; otherwise, false. |
Overrides
Item[Int32]
Gets the field at the specified index.
Declaration
public override string this[int field] { get; }
Parameters
System.Int32
field
|
Property Value
String
The field at the specified index. |
Overrides
Exceptions
ArgumentOutOfRangeException
|
System.InvalidOperationException
No record read yet. Call ReadLine() first. |
MalformedCsvException
The CSV appears to be corrupt at the current position. |
System.ObjectDisposedException
The instance has been disposed of. |
Item[Int32, Int32]
Gets the field at the specified index and record position.
Declaration
public string this[int record, int field] { get; }
Parameters
System.Int32
record
|
System.Int32
field
|
Property Value
String
The field at the specified index and record position. A null is returned if the field cannot be found for the record. |
Exceptions
ArgumentOutOfRangeException
|
ArgumentOutOfRangeException
Record index must be > 0. |
System.InvalidOperationException
Cannot move to a previous record in forward-only mode. |
System.IO.EndOfStreamException
Cannot read record at |
MalformedCsvException
The CSV appears to be corrupt at the current position. |
System.ObjectDisposedException
The instance has been disposed of. |
Item[Int32, String]
Gets the field with the specified name and record position. HasHeaders must be true.
Declaration
public string this[int record, string field] { get; }
Parameters
System.Int32
record
|
String
field
|
Property Value
String
The field with the specified name and record position. |
Exceptions
System.ArgumentNullException
|
System.InvalidOperationException
The CSV does not have headers (HasHeaders property is false). |
System.ArgumentException
|
ArgumentOutOfRangeException
Record index must be > 0. |
System.InvalidOperationException
Cannot move to a previous record in forward-only mode. |
System.IO.EndOfStreamException
Cannot read record at |
MalformedCsvException
The CSV appears to be corrupt at the current position. |
System.ObjectDisposedException
The instance has been disposed of. |
Item[String]
Gets the field with the specified name. HasHeaders must be true.
Declaration
public override string this[string field] { get; }
Parameters
String
field
|
Property Value
String
The field with the specified name. |
Overrides
Exceptions
System.ArgumentNullException
|
System.InvalidOperationException
The CSV does not have headers (HasHeaders property is false). |
System.ArgumentException
|
MalformedCsvException
The CSV appears to be corrupt at the current position. |
System.ObjectDisposedException
The instance has been disposed of. |
MissingFieldFlag
Indicates if one or more field are missing for the current record. Resets after each successful record read.
Declaration
public bool MissingFieldFlag { get; }
Property Value
Boolean
|
Options
Gets or sets the options of how to handle reading the csv file.
Declaration
public CsvReaderOptions Options { get; }
Property Value
CsvReaderOptions
|
ParseErrorFlag
Indicates if a parse error occured for the current record. Resets after each successful record read.
Declaration
public bool ParseErrorFlag { get; }
Property Value
Boolean
|
Methods
CopyCurrentRecordTo(String[])
Copies the field array of the current record to a one-dimensional array, starting at the beginning of the target array.
Declaration
public void CopyCurrentRecordTo(string[] array)
Parameters
String[]
array
The one-dimensional System.Array that is the destination of the fields of the current record. |
Exceptions
System.ArgumentNullException
|
CopyCurrentRecordTo(String[], Int32)
Copies the field array of the current record to a one-dimensional array, starting at the beginning of the target array.
Declaration
public void CopyCurrentRecordTo(string[] array, int index)
Parameters
String[]
array
The one-dimensional System.Array that is the destination of the fields of the current record. |
System.Int32
index
The zero-based index in |
Exceptions
System.ArgumentNullException
|
ArgumentOutOfRangeException
|
System.InvalidOperationException
No current record. |
System.ArgumentException
The number of fields in the record is greater than the available space from |
Dispose(Boolean)
Releases the unmanaged resources used by this instance and optionally releases the managed resources.
Declaration
protected override void Dispose(bool disposing)
Parameters
Boolean
disposing
true to release both managed and unmanaged resources; false to release only unmanaged resources. |
Overrides
GetAll<T>()
Reads all rows from the csv file and creates an instance of T for each row and initializes the instance properties from the reader.
Declaration
public List<T> GetAll<T>()
where T : class, new()
Returns
List<T>
A List<T> consisting of all the columns and rows in the csv-file. |
Type Parameters
T
The System.Type of object to fill from this reader. Must be exact same as supplied as type when constructing the reader. |
GetByte(Int32)
Gets the 8-bit unsigned integer value of the specified column.
Declaration
public override byte GetByte(int i)
Parameters
System.Int32
i
The zero-based column ordinal. |
Returns
System.Byte
The 8-bit unsigned integer value of the specified column. |
Overrides
GetBytes(Int32, Int64, Byte[], Int32, Int32)
Reads a stream of bytes from the specified column offset into the buffer as an array, starting at the given buffer offset.
Declaration
public override long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
Parameters
System.Int32
i
The zero-based column ordinal. |
System.Int64
fieldOffset
The index within the field from which to start the read operation. |
System.Byte[]
buffer
The buffer into which to read the stream of bytes. |
System.Int32
bufferoffset
The index for buffer to start the read operation. |
System.Int32
length
The number of bytes to read. |
Returns
System.Int64
The actual number of bytes read. |
Overrides
GetChars(Int32, Int64, Char[], Int32, Int32)
Reads a stream of characters from the specified column offset into the buffer as an array, starting at the given buffer offset.
Declaration
public override long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
Parameters
System.Int32
i
The zero-based column ordinal. |
System.Int64
fieldoffset
The index within the row from which to start the read operation. |
Char[]
buffer
The buffer into which to read the stream of bytes. |
System.Int32
bufferoffset
The index for buffer to start the read operation. |
System.Int32
length
The number of bytes to read. |
Returns
System.Int64
The actual number of characters read. |
Overrides
GetCurrentRawData()
Gets the current raw CSV data.
Declaration
public string GetCurrentRawData()
Returns
String
The current raw CSV data. |
Remarks
Used for exception handling purpose.
GetEnumerator()
Returns an CsvDataReader.RecordEnumerator that can iterate through CSV records.
Declaration
public CsvDataReader.RecordEnumerator GetEnumerator()
Returns
CsvDataReader.RecordEnumerator
An CsvDataReader.RecordEnumerator that can iterate through CSV records. |
Exceptions
System.ObjectDisposedException
The instance has been disposed of. |
GetFieldIndex(String)
Gets the field index for the provided header.
Declaration
public int GetFieldIndex(string header)
Parameters
String
header
The header to look for. |
Returns
System.Int32
The field index for the provided header. -1 if not found. |
Exceptions
System.ObjectDisposedException
The instance has been disposed of. |
GetFieldType(Int32)
Gets the System.Type information corresponding to the type of System.Object that would be returned from System.Data.IDataRecord.GetValue(System.Int32).
Declaration
public override Type GetFieldType(int i)
Parameters
System.Int32
i
The index of the field to find. |
Returns
System.Type
The System.Type information corresponding to the type of System.Object that would be returned from System.Data.IDataRecord.GetValue(System.Int32). |
Overrides
GetName(Int32)
Gets the name for the field to find.
Declaration
public override string GetName(int i)
Parameters
System.Int32
i
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. |
Overrides
GetOrdinal(String)
Return the index of the named field.
Declaration
public override int GetOrdinal(string name)
Parameters
String
name
The name of the field to find. |
Returns
System.Int32
The index of the named field. |
Overrides
GetSchemaTable()
Returns a System.Data.DataTable that describes the column metadata of the System.Data.IDataReader.
Declaration
public override DataTable GetSchemaTable()
Returns
System.Data.DataTable
A System.Data.DataTable that describes the column metadata. |
Overrides
GetValue(Int32)
Return the value of the specified field.
Declaration
public override object GetValue(int i)
Parameters
System.Int32
i
The index of the field to find. |
Returns
Object
The System.Object which will contain the field value upon return. |
Overrides
GetValues<T>()
Gets a complete object with all properties set from the reader. This method requires that the reader is constructed with a supplied System.Type which must be the same as T.
Declaration
public T GetValues<T>()
where T : class, new()
Returns
T
An object with all properties set from the reader. |
Type Parameters
T
The System.Type of object to fill from this reader. Must be exact same as supplied as type when constructing the reader. |
MoveTo(Int64)
Moves to the specified record index.
Declaration
public virtual bool MoveTo(long record)
Parameters
System.Int64
record
The record index. |
Returns
Boolean
|
Exceptions
System.ObjectDisposedException
The instance has been disposed of. |
OnParseError(ParseErrorEventArgs)
Raises the ParseError event.
Declaration
protected virtual void OnParseError(ParseErrorEventArgs e)
Parameters
ParseErrorEventArgs
e
The ParseErrorEventArgs that contains the event data. |
Read()
Advances the System.Data.IDataReader to the next record.
Declaration
public override bool Read()
Returns
Boolean
true if there are more rows; otherwise, false. |
Overrides
ReadNextRecord()
Reads the next record.
Declaration
public bool ReadNextRecord()
Returns
Boolean
true if a record has been successfully reads; otherwise, false. |
Exceptions
System.ObjectDisposedException
The instance has been disposed of. |
ReadNextRecord(Boolean, Boolean)
Reads the next record.
Declaration
protected virtual bool ReadNextRecord(bool onlyReadHeaders, bool skipToNextLine)
Parameters
Boolean
onlyReadHeaders
Indicates if the reader will proceed to the next record after having read headers. true if it stops after having read headers; otherwise, false. |
Boolean
skipToNextLine
Indicates if the reader will skip directly to the next line without parsing the current one. To be used when an error occurs. |
Returns
Boolean
true if a record has been successfully reads; otherwise, false. |
Exceptions
System.ObjectDisposedException
The instance has been disposed of. |
Events
ParseError
Occurs when there is an error while parsing the CSV stream.
Declaration
public event EventHandler<ParseErrorEventArgs> ParseError
Event Type
System.EventHandler<ParseErrorEventArgs>
|
Explicit Interface Implementations
IEnumerable<String[]>.GetEnumerator()
Returns an System.Collections.Generic.IEnumerator<T> that can iterate through CSV records.
Declaration
IEnumerator<string[]> IEnumerable<string[]>.GetEnumerator()
Returns
System.Collections.Generic.IEnumerator<String[]>
An System.Collections.Generic.IEnumerator<T> that can iterate through CSV records. |
Implements
Exceptions
System.ObjectDisposedException
The instance has been disposed of. |
IEnumerable.GetEnumerator()
Returns an System.Collections.Generic.IEnumerator<T> that can iterate through CSV records.
Declaration
IEnumerator IEnumerable.GetEnumerator()
Returns
System.Collections.IEnumerator
An System.Collections.Generic.IEnumerator<T> that can iterate through CSV records. |
Implements
Exceptions
System.ObjectDisposedException
The instance has been disposed of. |