Table of Contents

Class DataTransfer

Namespace
Cuemon.Data
Assembly
Cuemon.Data.dll

Provides a way to convert an IDataReader implementation to a table-like data transfer object.

public static class DataTransfer
Inheritance
DataTransfer

Examples

The following example demonstrates how to use to convert an into row-based and column-based collections.

using System;
using System.Data;
using Cuemon.Data;

namespace MyApp.Data;

public sealed class DataTransferExample
{
    public void Demonstrate()
    {
        var table = new DataTable("Products");
        table.Columns.Add("Id", typeof(int));
        table.Columns.Add("Name", typeof(string));
        table.Columns.Add("Price", typeof(decimal));
        table.Rows.Add(1, "Widget", 9.99m);
        table.Rows.Add(2, "Gadget", 24.95m);

        using IDataReader reader = table.CreateDataReader();

        // Convert reader rows to a collection
        DataTransferRowCollection rows = DataTransfer.GetRows(reader);
        Console.WriteLine($"Row count: {rows.Count}");
        Console.WriteLine($"Columns: {string.Join(", ", rows.ColumnNames)}");

        // Access data by column name
        foreach (DataTransferRow row in rows)
        {
            Console.WriteLine($"{row["Id"]}: {row["Name"]} @ {row["Price"]:C}");
        }

        // Re-read and get columns
        reader.Dispose();
        using IDataReader reader2 = table.CreateDataReader();
        reader2.Read();
        DataTransferColumnCollection columns = DataTransfer.GetColumns(reader2);
        Console.WriteLine($"Column count: {columns.Count}");
        Console.WriteLine($"First column: {columns[0].Name} ({columns[0].DataType.Name})");
    }
}

Methods

GetColumns(IDataReader)

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

public static DataTransferColumnCollection GetColumns(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.

GetRows(IDataReader)

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

public static DataTransferRowCollection GetRows(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.