Tutorials ADO.NET Core Tutorial
Streaming Data — Complete Guide
Streaming Data — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of ADO.NET Core Tutorial on Toolliyo Academy.
On this page
ADO.NET Core Tutorial · Lesson 46 of 100
Streaming Data
Foundations ✓ → SQL & safety → Production → Projects
SQL & safety · 2 — Procs, tx, performance · ~6 min · Module 5: Performance Optimization
What is this?
Stream query results to HTTP with IAsyncEnumerable or pipes so memory stays flat.
Why should you care?
ShopNest exports of 2M order lines must stream.
See it live — copy this example
Use a .NET console or API project with SQL Server LocalDB. Run dotnet run after pasting.
async IAsyncEnumerable<OrderDto> StreamAsync([EnumeratorCancellation] CancellationToken ct)
{
await using var conn = new SqlConnection(cs);
await conn.OpenAsync(ct);
await using var cmd = new SqlCommand("SELECT Id, Total FROM Orders", conn);
await using var r = await cmd.ExecuteReaderAsync(ct);
while (await r.ReadAsync(ct))
yield return new OrderDto(r.GetInt32(0), r.GetDecimal(1));
}
What happened?
- yield return from reader.
- Minimal APIs can return IAsyncEnumerable.
- Keep connection alive for the stream lifetime carefully.
Practice next
- build StreamAsync.
- Consume with await foreach.
- Cancel mid-stream.
- Map more columns.
- Write CSV while streaming.
Remember
IAsyncEnumerable streams. Don’t buffer all. Honor cancellation.
ShopNest export endpoint
GET /exports/orders streams DTOs.
Outcome: Memory flat at millions of rows.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!