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
readerIDataReaderThe read-initialized reader to be converted.
Returns
- DataTransferColumnCollection
A DataTransferColumnCollection that is the result of the specified and read-initialized
reader.
Exceptions
- ArgumentNullException
readeris null.- ArgumentException
readeris closed.- InvalidOperationException
Invalid attempt to read from
readerwhen 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
readerIDataReaderThe reader to be converted.
Returns
- DataTransferRowCollection
A DataTransferRowCollection that is the result of the specified
reader.
Exceptions
- ArgumentNullException
readeris null.- ArgumentException
readeris closed.