MSDN Documentation

DataReader Concepts

The DataReader object in ADO.NET provides a way to retrieve a forward-only, read-only stream of data from a data source. It is highly efficient for reading large amounts of data quickly, as it doesn't load the entire dataset into memory at once. This makes it ideal for scenarios where you need to process records one by one.

Key Features and Benefits:

Commonly Used Methods:

Example Usage (C#):

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

public class DataReaderExample
{
    public static void Main(string[] args)
    {
        string connectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;";

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

            using (SqlCommand command = new SqlCommand(sql, connection))
            {
                // Execute the command and get a DataReader
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    // Check if there are any rows
                    if (reader.HasRows)
                    {
                        // Loop through each row
                        while (reader.Read())
                        {
                            // Access data by column ordinal
                            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 data by column name (less efficient)
                            // int customerIdByName = reader.GetInt32(reader.GetOrdinal("CustomerID"));
                            // string companyNameByName = reader.GetString(reader.GetOrdinal("CompanyName"));
                        }
                    }
                    else
                    {
                        Console.WriteLine("No rows found.");
                    }
                } // DataReader is automatically closed here
            }
        } // Connection is automatically closed here
    }
}

Important Note on Performance

When accessing data by column ordinal (index), it's generally faster than accessing by column name. If you need to access columns by name frequently, consider using reader.GetOrdinal("ColumnName") once and storing the ordinal for repeated use.

DataReader vs. DataTable

While both are used to retrieve data, they serve different purposes:

Performance Tip

Always ensure you close your DataReader as soon as you are finished with it. The using statement is the recommended way to guarantee proper disposal and prevent resource leaks.