DbDataReader Streaming Serialization

A C# serialization layer that streams DbDataReader results directly to JSON or XML through typed getters, avoiding DataTable/DTO materialization and resolving stable schema metadata once.

The optimisation target here is the data path around serialization, not merely the serializer call itself. Materializing a large result set into a DataTable, a List<DTO> or dictionaries creates an intermediate object graph that then has to be walked again. This implementation connects DbDataReader directly to Utf8JsonWriter or XmlWriter.

Column names, type decisions and other stable schema information are prepared before the repeated row path. JSON names can be pre-encoded and common CLR types use typed getters. DbRowView deliberately reuses one view object as the reader advances instead of allocating a wrapper per row; consumers therefore must not retain it as if it were an immutable snapshot.

Very large LOB values are a separate problem. The scalar path is not advertised as a replacement for provider-level GetStream or GetTextReader access. A related design principle appears in CSV Enumerable, where header/property mapping is also performed once before streaming rows.

Design article: Streaming JSON and XML Serialization from DbDataReader Repository: GitHub Zenodo release: Zenodo DOI: DOI

QR code for this page