Show / Hide Table of Contents

Class CsvDataReader

Represents a reader that provides fast, non-cached, forward-only access to CSV data.

Inheritance
Object
BaseDataReader
CsvDataReader
Inherited Members
BaseDataReader.NextResult()
BaseDataReader.Close()
BaseDataReader.Depth
BaseDataReader.RecordsAffected
BaseDataReader.IDataRecord.Item[Int32]
BaseDataReader.IDataRecord.Item[String]
BaseDataReader.GetBoolean(Int32)
BaseDataReader.GetChar(Int32)
BaseDataReader.GetData(Int32)
BaseDataReader.GetDataTypeName(Int32)
BaseDataReader.GetDateTime(Int32)
BaseDataReader.GetDecimal(Int32)
BaseDataReader.GetDouble(Int32)
BaseDataReader.GetFloat(Int32)
BaseDataReader.GetGuid(Int32)
BaseDataReader.GetInt16(Int32)
BaseDataReader.GetInt32(Int32)
BaseDataReader.GetInt64(Int32)
BaseDataReader.GetString(Int32)
BaseDataReader.GetValues(Object[])
BaseDataReader.IsDBNull(Int32)
BaseDataReader.GetNullableInt32(Int32)
BaseDataReader.Disposed
BaseDataReader.IsDisposed
BaseDataReader.OnDisposed(EventArgs)
BaseDataReader.CheckDisposed()
BaseDataReader.Dispose()
System.Object.ToString()
Object.Equals(Object)
System.Object.Equals(System.Object, System.Object)
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
Object.MemberwiseClone()
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

reader is a null or options is a null.

System.ArgumentException

Cannot read from reader.

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

reader is a null or options is a null.

ArgumentOutOfRangeException

bufferSize must be 1 or more.

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

reader is a null or options is a null.

System.ArgumentException

Cannot read from reader.

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 type ska användas.

CsvReaderOptions options

A CsvReaderOptions object defining how to handle reading the csv file.

Exceptions
System.ArgumentNullException

reader is a null or options is a null.

System.ArgumentException

Cannot read from reader.

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
BaseDataReader.FieldCount
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
BaseDataReader.IsClosed

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
BaseDataReader.Item[Int32]
Exceptions
ArgumentOutOfRangeException

field must be included in [0, FieldCount[.

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

field must be included in [0, FieldCount[.

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 record.

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

field is null or an empty string.

System.InvalidOperationException

The CSV does not have headers (HasHeaders property is false).

System.ArgumentException

field not found.

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 record.

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
BaseDataReader.Item[String]
Exceptions
System.ArgumentNullException

field is null or an empty string.

System.InvalidOperationException

The CSV does not have headers (HasHeaders property is false).

System.ArgumentException

field not found.

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

array is null.

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 array at which copying begins.

Exceptions
System.ArgumentNullException

array is null.

ArgumentOutOfRangeException

index is les than zero or is equal to or greater than the length array.

System.InvalidOperationException

No current record.

System.ArgumentException

The number of fields in the record is greater than the available space from index to the end of array.

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
BaseDataReader.Dispose(Boolean)

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
BaseDataReader.GetByte(Int32)

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
BaseDataReader.GetBytes(Int32, Int64, Byte[], Int32, Int32)

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
BaseDataReader.GetChars(Int32, Int64, Char[], Int32, Int32)

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
BaseDataReader.GetFieldType(Int32)

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
BaseDataReader.GetName(Int32)

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
BaseDataReader.GetOrdinal(String)

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
BaseDataReader.GetSchemaTable()

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
BaseDataReader.GetValue(Int32)

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

true if the operation was successful; otherwise, false.

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
BaseDataReader.Read()

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
IEnumerable<T>.GetEnumerator()
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
System.Collections.IEnumerable.GetEnumerator()
Exceptions
System.ObjectDisposedException

The instance has been disposed of.

Extension Methods

EnumerableExtensions.GroupAdjacentBy<T>(IEnumerable<T>, Func<T, T, Boolean>)
EnumerableExtensions.Each<T>(IEnumerable<T>, Action<T>)
EnumerableExtensions.IsSequenceBroken<TSource, TResult>(IEnumerable<TSource>, Func<TSource, TResult>, Boolean)
EnumerableExtensions.FindMissingNumbersInSequence<TSource, TResult>(IEnumerable<TSource>, Func<TSource, TResult>, Boolean)
EnumerableExtensions.FindNumbersBeforeNumbersOutOfSequence<TSource, TResult>(IEnumerable<TSource>, Func<TSource, TResult>, Boolean)
EnumerableExtensions.FindNumbersOutOfSequence<TSource, TResult>(IEnumerable<TSource>, Func<TSource, TResult>, Boolean)
ObjectExtensions.DeepClone<T>(T)
DataReaderExtensions.ReadAs<T>(IDataReader, Func<IDataRecord, T>)
DataReaderExtensions.ReadAs<T>(IDataReader, Func<IDataRecord, T>, Action<T>)
DataRecordExtensions.Parse<T>(IDataRecord, Int32, Func<IDataRecord, Int32, T>)
DataRecordExtensions.Parse<T>(IDataRecord, String, Func<IDataRecord, String, T>)
DataRecordExtensions.ParsePersonnummer(IDataRecord, Int32)
DataRecordExtensions.ParseBoolean(IDataRecord, Int32)
DataRecordExtensions.ParseBoolean(IDataRecord, String)
DataRecordExtensions.ParseBooleanValueFromZeroOrOne(IDataRecord, Int32)
DataRecordExtensions.ParseNullableBoolean(IDataRecord, Int32)
DataRecordExtensions.ParseNullableBoolean(IDataRecord, String)
DataRecordExtensions.ParseDateTime(IDataRecord, Int32, String)
DataRecordExtensions.ParseDateTime(IDataRecord, Int32)
DataRecordExtensions.ParseDateTime(IDataRecord, Int32, IFormatProvider, DateTimeStyles)
DataRecordExtensions.ParseDateTime(IDataRecord, String)
DataRecordExtensions.ParseDateTime(IDataRecord, String, IFormatProvider, DateTimeStyles)
DataRecordExtensions.ParseDateTimeExact(IDataRecord, Int32, String, IFormatProvider, DateTimeStyles)
DataRecordExtensions.ParseDateTimeExact(IDataRecord, Int32, String[], IFormatProvider, DateTimeStyles)
DataRecordExtensions.ParseDateTimeExact(IDataRecord, String, String, IFormatProvider, DateTimeStyles)
DataRecordExtensions.ParseDateTimeExact(IDataRecord, String, String[], IFormatProvider, DateTimeStyles)
DataRecordExtensions.ParseNullableDateTime(IDataRecord, Int32)
DataRecordExtensions.ParseNullableDateTime(IDataRecord, Int32, IFormatProvider, DateTimeStyles)
DataRecordExtensions.ParseNullableDateTime(IDataRecord, String)
DataRecordExtensions.ParseNullableDateTime(IDataRecord, String, IFormatProvider, DateTimeStyles)
DataRecordExtensions.ParseNullableDateTimeExact(IDataRecord, Int32, String, IFormatProvider, DateTimeStyles)
DataRecordExtensions.ParseNullableDateTimeExact(IDataRecord, Int32, String[], IFormatProvider, DateTimeStyles)
DataRecordExtensions.ParseNullableDateTimeExact(IDataRecord, String, String, IFormatProvider, DateTimeStyles)
DataRecordExtensions.ParseNullableDateTimeExact(IDataRecord, String, String[], IFormatProvider, DateTimeStyles)
DataRecordExtensions.ParseDecimal(IDataRecord, Int32)
DataRecordExtensions.ParseDecimal(IDataRecord, Int32, NumberStyles, IFormatProvider)
DataRecordExtensions.ParseDecimal(IDataRecord, String)
DataRecordExtensions.ParseDecimal(IDataRecord, String, NumberStyles, IFormatProvider)
DataRecordExtensions.ParseNullableDecimal(IDataRecord, Int32)
DataRecordExtensions.ParseNullableDecimal(IDataRecord, Int32, NumberStyles, IFormatProvider)
DataRecordExtensions.ParseNullableDecimal(IDataRecord, String)
DataRecordExtensions.ParseNullableDecimal(IDataRecord, String, NumberStyles, IFormatProvider)
DataRecordExtensions.ParseDouble(IDataRecord, Int32)
DataRecordExtensions.ParseDouble(IDataRecord, Int32, NumberStyles, IFormatProvider)
DataRecordExtensions.ParseDouble(IDataRecord, String)
DataRecordExtensions.ParseDouble(IDataRecord, String, NumberStyles, IFormatProvider)
DataRecordExtensions.ParseNullableDouble(IDataRecord, Int32)
DataRecordExtensions.ParseNullableDouble(IDataRecord, Int32, NumberStyles, IFormatProvider)
DataRecordExtensions.ParseNullableDouble(IDataRecord, String)
DataRecordExtensions.ParseNullableDouble(IDataRecord, String, NumberStyles, IFormatProvider)
DataRecordExtensions.ParseInt(IDataRecord, Int32)
DataRecordExtensions.ParseInt(IDataRecord, Int32, NumberStyles, IFormatProvider)
DataRecordExtensions.ParseInt(IDataRecord, String)
DataRecordExtensions.ParseInt(IDataRecord, String, NumberStyles, IFormatProvider)
DataRecordExtensions.ParseNullableInt(IDataRecord, Int32)
DataRecordExtensions.ParseNullableInt(IDataRecord, Int32, NumberStyles, IFormatProvider)
DataRecordExtensions.ParseNullableInt(IDataRecord, String)
DataRecordExtensions.ParseNullableInt(IDataRecord, String, NumberStyles, IFormatProvider)
DataRecordExtensions.ParseLong(IDataRecord, Int32)
DataRecordExtensions.ParseLong(IDataRecord, Int32, NumberStyles, IFormatProvider)
DataRecordExtensions.ParseLong(IDataRecord, String)
DataRecordExtensions.ParseLong(IDataRecord, String, NumberStyles, IFormatProvider)
DataRecordExtensions.ParseNullableLong(IDataRecord, Int32)
DataRecordExtensions.ParseNullableLong(IDataRecord, Int32, NumberStyles, IFormatProvider)
DataRecordExtensions.ParseNullableLong(IDataRecord, String)
DataRecordExtensions.ParseNullableLong(IDataRecord, String, NumberStyles, IFormatProvider)
DataRecordExtensions.ParseShort(IDataRecord, Int32)
DataRecordExtensions.ParseShort(IDataRecord, Int32, NumberStyles, IFormatProvider)
DataRecordExtensions.ParseShort(IDataRecord, String)
DataRecordExtensions.ParseShort(IDataRecord, String, NumberStyles, IFormatProvider)
DataRecordExtensions.ParseNullableShort(IDataRecord, Int32)
DataRecordExtensions.ParseNullableShort(IDataRecord, Int32, NumberStyles, IFormatProvider)
DataRecordExtensions.ParseNullableShort(IDataRecord, String)
DataRecordExtensions.ParseNullableShort(IDataRecord, String, NumberStyles, IFormatProvider)
DataRecordExtensions.GetValue<TType>(IDataRecord, String, Boolean)
DataRecordExtensions.GetValue<TType>(IDataRecord, Int32, Boolean)
AnnotationsExtensions.IsValid(Object)
Back to top Generated by DocFX