Table of Contents

Class DataReaderExtensions

Namespace
Cuemon.Extensions.Data
Assembly
Cuemon.Extensions.Data.dll

Extension methods for the IDataReader interface.

public static class DataReaderExtensions
Inheritance
DataReaderExtensions

Examples

The following example demonstrates how to turn a delimiter-separated reader into row and column transfer objects.

using System;
using System.IO;
using System.Linq;
using System.Text;
using Cuemon.Data;
using Cuemon.Extensions.Data;

namespace MyApp.Examples;

public static class DataReaderExtensionsExample
{
    public static void Demonstrate()
    {
        var csv = "Id,Name\n1,Alice\n2,Bob";
        using var reader = new DsvDataReader(new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(csv))));

        var rows = reader.ToRows();
        Console.WriteLine(rows.Count);
        Console.WriteLine(rows.ColumnNames.Contains("Name"));

        using var columnReader = new DsvDataReader(new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(csv))));
        columnReader.Read();
        var columns = columnReader.ToColumns();

        Console.WriteLine(columns.Count);
    }
}

Methods

ToColumns(IDataReader)

Converts the specified and read-initialized reader implementation to a column-like data transfer object.

public static DataTransferColumnCollection ToColumns(this IDataReader reader)

Parameters

reader IDataReader

The read-initialized reader to be converted.

Returns

DataTransferColumnCollection

A DataTransferColumnCollection that is the result of the specified and read-initialized reader.

Exceptions

ArgumentNullException

reader is null.

ArgumentException

reader is closed.

InvalidOperationException

Invalid attempt to read from reader when no data is present.

ToRows(IDataReader)

Converts the specified reader implementation to a table-like data transfer object.

public static DataTransferRowCollection ToRows(this IDataReader reader)

Parameters

reader IDataReader

The reader to be converted.

Returns

DataTransferRowCollection

A DataTransferRowCollection that is the result of the specified reader.

Exceptions

ArgumentNullException

reader is null.

ArgumentException

reader is closed.