MSDN Documentation

ADO.NET Commands and DataReaders

The ADO.NET Command and DataReader objects are fundamental components for interacting with databases. Commands are used to execute SQL statements or stored procedures, while DataReaders provide a fast, forward-only, read-only stream of data from a database.

ADO.NET Commands

The Command object represents a SQL statement or stored procedure to be executed against a data source. It can be created for a specific data provider (e.g., SqlCommand for SQL Server, OracleCommand for Oracle).

Key properties of a Command object include:

Command Types

The CommandType enumeration defines how the CommandText is interpreted:

Executing Commands

Commands can be executed in various ways, depending on the expected outcome:

Note: Always close your DataReader and Connection objects when you are finished with them to release resources. Using using statements (in C#) or Try...Finally blocks (in VB.NET) is highly recommended for proper resource management.

DataReaders

The DataReader object (e.g., SqlDataReader) provides a way to read a forward-only stream of rows from a data source. It's the most performant way to retrieve data when you only need to read through the results sequentially.

DataReader objects are lightweight and do not load the entire result set into memory, making them ideal for large datasets.

Reading Data

The primary method for advancing through the result set with a DataReader is Read(). This method returns true if there are more rows to read and advances the reader to the next row.

You can access data in the current row by column name or by ordinal index.


// Example using SqlCommand and SqlDataReader (C#)
using System;
using System.Data;
using System.Data.SqlClient;

public class DataReaderExample
{
    public static void Main(string[] args)
    {
        string connectionString = "YourConnectionStringHere";
        string sql = "SELECT CustomerID, CompanyName FROM Customers;";

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            using (SqlCommand command = new SqlCommand(sql, connection))
            {
                try
                {
                    connection.Open();
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                // Access data by column name
                                int customerId = reader.GetInt32(reader.GetOrdinal("CustomerID"));
                                string companyName = reader.GetString(reader.GetOrdinal("CompanyName"));

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

                                // Alternatively, access by ordinal index (0-based)
                                // int customerIdByIndex = reader.GetInt32(0);
                                // string companyNameByIndex = reader.GetString(1);
                            }
                        }
                        else
                        {
                            Console.WriteLine("No rows found.");
                        }
                    }
                }
                catch (SqlException ex)
                {
                    Console.WriteLine($"Error: {ex.Message}");
                }
            }
        }
    }
}
            

Important Considerations

IDataReader Interface Members (Common Methods)

  • FieldCount: Gets the number of columns in the current row.
  • GetOrdinal(string name): Returns the column ordinal (0-based index) of the specified column name.
  • GetValue(int i): Gets the value of the specified column.
  • IsDBNull(int i): Returns a value indicating whether the column contains a null value.
  • Read(): Advances the IDataReader to the next record.
  • Close(): Closes the DataReader object.