Microsoft Developer Network (MSDN)

Documentation | .NET | APIs | Data Access | ADO.NET

IDataReader Interface

The IDataReader interface provides a way to retrieve a forward-only, read-only stream of data from a data source. It is the most efficient way to retrieve data when you need to iterate through the results one record at a time without needing to load the entire dataset into memory. This is particularly useful for large result sets.

Overview

When you execute a command that returns a result set using an ADO.NET data provider (like SqlClient for SQL Server or OleDbClient for OLE DB sources), you can obtain an IDataReader object. You then use the methods of the IDataReader to advance through the records and access the data within each column.

Key Features and Benefits

Commonly Used Methods

Example Usage (C#)

Here's a typical pattern for using IDataReader to retrieve data from a SQL Server database.


using System;
using System.Data;
using System.Data.SqlClient;

public class DataReaderExample
{
    public static void Main(string[] args)
    {
        string connectionString = "Server=your_server_name;Database=your_database;User ID=your_user;Password=your_password;";
        string query = "SELECT CustomerID, CompanyName, ContactName FROM Customers";

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            using (SqlCommand command = new SqlCommand(query, connection))
            {
                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    // Check if there are any rows
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            // Access data by column ordinal (index)
                            int customerId = reader.GetInt32(0);
                            string companyName = reader.GetString(1);
                            string contactName = reader.IsDBNull(2) ? "N/A" : reader.GetString(2);

                            Console.WriteLine($"ID: {customerId}, Company: {companyName}, Contact: {contactName}");

                            // Alternatively, access by column name
                            // int companyNameOrdinal = reader.GetOrdinal("CompanyName");
                            // string companyNameByName = reader.GetString(companyNameOrdinal);
                            // Console.WriteLine($"Company (by name): {companyNameByName}");
                        }
                    }
                    else
                    {
                        Console.WriteLine("No rows found.");
                    }
                } // Reader is automatically closed here
            }
        } // Connection is automatically closed here
    }
}

            

Important Considerations

Resource Management: Always ensure that the IDataReader and the associated Connection are properly closed to release database resources. The using statement in C# is the recommended way to achieve this automatically.

Null Values: Before accessing a column's value, it's good practice to check if it's a DBNull using IsDBNull(ordinal) to avoid runtime errors.

Performance Comparison with DataSet

While DataSet provides rich functionality for disconnected data manipulation (caching, multiple tables, relationships), it loads the entire result set into memory. For scenarios where you simply need to read data sequentially and process it, IDataReader offers superior performance and reduced memory footprint. If you need to perform operations like sorting, filtering, or updating disconnected data, DataSet or LINQ to Objects might be more appropriate.