MSDN Documentation

ADO.NET DataReaders

ADO.NET DataReaders provide a way to retrieve a forward-only, read-only stream of data from a data source. They are highly efficient for scenarios where you need to process data record by record without needing to load the entire result set into memory. This makes them ideal for scenarios like populating controls that display data sequentially or performing operations on large result sets.

Overview of DataReaders

A DataReader object, typically represented by classes like SqlDataReader or OleDbDataReader, exposes a method called Read(). This method advances the reader to the next record in the result set. While Read() returns true if there is another record to read, it returns false when the end of the result set is reached.

The primary benefit of using DataReaders is their performance. Because they retrieve data one record at a time and do not require buffering the entire result set, they consume less memory and can be faster for large datasets.

Key Properties and Methods

Example Usage

Here's a simple C# example demonstrating how to use a SqlDataReader:


using System;
using System.Data.SqlClient;

// Assuming you have a valid connection string
string connectionString = "YourConnectionStringHere";

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    string query = "SELECT CustomerID, CompanyName FROM Customers";

    using (SqlCommand command = new SqlCommand(query, connection))
    {
        using (SqlDataReader reader = command.ExecuteReader())
        {
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    // Access data by ordinal (index)
                    int customerId = reader.GetInt32(0);
                    string companyName = reader.GetString(1);

                    Console.WriteLine($"ID: {customerId}, Name: {companyName}");

                    // Alternatively, access by column name
                    // string companyNameByName = reader["CompanyName"].ToString();
                    // Console.WriteLine($"Name (by name): {companyNameByName}");
                }
            }
            else
            {
                Console.WriteLine("No rows found.");
            }
        } // SqlDataReader is automatically closed here
    }
} // SqlConnection is automatically closed here
            
Important: When using DataReaders, it is crucial to close them as soon as you are finished reading the data. Closing the DataReader also releases the underlying database connection, making it available for other operations. The using statement in C# is highly recommended for ensuring that both the DataReader and the connection are properly disposed of, even if exceptions occur.

When to Use DataReaders

DataReaders vs. Datasets

While both DataReaders and Datasets are part of ADO.NET for data access, they serve different purposes:

The choice between DataReaders and Datasets depends on your specific application requirements. If you need a simple, efficient way to read data, use a DataReader. If you need to work with data offline, manipulate it, and then send changes back to the database, a Dataset is more appropriate.