Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • Security
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
    • SEO Analyzer
    • Background Remover
  1. Home
  2. Blog
  3. C#
  4. DataReader in ADO.NET

DataReader in ADO.NET

Date- May 31,2023

Updated Mar 2026

4855

DataReader in ADONET csharp

Overview of ADO.NET DataReader

The DataReader class in ADO.NET is designed for high-performance data retrieval. It is ideal for applications where speed is crucial, and the data is only needed for reading. Unlike the DataSet, which is a more complex object that allows for data manipulation and caching, the DataReader provides a lightweight way to access data directly from the database.

One of the key advantages of using the DataReader is its ability to stream data directly from the database, minimizing memory usage. This is particularly beneficial in scenarios involving large datasets where loading all data into memory would be inefficient. The DataReader works in a forward-only manner, meaning that once you read a row, you cannot go back to it unless you re-execute the query.

Using DataReader in C#

To utilize the DataReader effectively, you need to establish a connection to your database and execute a command that retrieves data. Below is a more detailed example of using the DataReader in a C# application:

using System;
using System.Data.SqlClient;

class Program {
    static void Main() {
        string connectionString = "YourConnectionString";
        string query = "SELECT * FROM Customers";

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

            using (SqlDataReader reader = command.ExecuteReader()) {
                while (reader.Read()) {
                    int customerId = reader.GetInt32(0);
                    string customerName = reader.GetString(1);
                    string address = reader.GetString(2);

                    Console.WriteLine($"Customer ID: {customerId}");
                    Console.WriteLine($"Customer Name: {customerName}");
                    Console.WriteLine($"Address: {address}");
                    Console.WriteLine();
                }
            }
        }
    }
}

In this example, we define a connection string and an SQL query to select all records from the Customers table. The connection is established using the SqlConnection class, and the query is executed with the SqlCommand class. After opening the connection, the ExecuteReader() method is called to obtain a SqlDataReader object, which we then use to read the data row by row.

DataReader Methods

The SqlDataReader class provides several methods for retrieving data. Some of the most commonly used methods include:

  • GetInt32(int ordinal): Retrieves a 32-bit signed integer from the specified column.
  • GetString(int ordinal): Retrieves a string from the specified column.
  • GetValue(int ordinal): Retrieves the value of the specified column as an object.
  • HasRows: Indicates whether the SqlDataReader contains any rows.

It is important to use the appropriate method corresponding to the data type of the column you are accessing. Using the wrong method can lead to runtime exceptions.

Handling Exceptions

When working with databases, it is crucial to handle exceptions effectively to ensure the application remains robust. Common exceptions that may arise when using DataReader include:

  • SqlException: Raised when SQL Server returns a warning or error.
  • InvalidOperationException: Raised when the operation is not valid for the current state of the SqlDataReader.

To handle these exceptions, you can use a try-catch block as shown in the following code example:

try {
    using (SqlDataReader reader = command.ExecuteReader()) {
        // Reading data...
    }
} catch (SqlException ex) {
    Console.WriteLine($"SQL Error: {ex.Message}");
} catch (InvalidOperationException ex) {
    Console.WriteLine($"Operation Error: {ex.Message}");
}

Edge Cases & Gotchas

When using the DataReader, there are several edge cases and gotchas to be aware of:

  • Closed Connections: Ensure that the connection remains open while reading data. If the connection is closed before reading, an exception will be thrown.
  • Data Types: Be cautious about data types. If you attempt to read a value as the wrong type, it will result in an exception.
  • Null Values: Always check for null values in the database to avoid exceptions when retrieving data. Use reader.IsDBNull() to check if a column value is null.

Here's an example of checking for null values:

if (!reader.IsDBNull(2)) {
    string address = reader.GetString(2);
}

Performance & Best Practices

To maximize the performance of your application when using the DataReader, consider the following best practices:

  • Use CommandBehavior: Specify the CommandBehavior when executing the reader to optimize performance. For example, use CommandBehavior.CloseConnection to automatically close the connection when the reader is closed.
  • Limit Data Retrieval: Only select the columns you need instead of using SELECT *. This reduces the amount of data transferred and improves performance.
  • Use Asynchronous Methods: For applications that require high responsiveness, consider using asynchronous methods such as ExecuteReaderAsync() to avoid blocking the main thread.

Conclusion

In summary, the DataReader class in ADO.NET is a powerful and efficient way to read data from a database in a forward-only and read-only manner. By understanding its methods, handling exceptions, and following best practices, developers can effectively utilize this class in their applications.

  • DataReader is ideal for performance-sensitive applications.
  • Always check for null values to prevent exceptions.
  • Use CommandBehavior to optimize data retrieval.
  • Handle exceptions gracefully to ensure application stability.

S
Shubham Batra
Programming author at Code2Night โ€” sharing tutorials on ASP.NET, C#, and more.
View all posts โ†’

Related Articles

Mastering Async and Await in C#: A Comprehensive Guide
Mar 16, 2026
How to Use Stored Procedures with Parameters in Dapper
Jan 23, 2024
Mastering LINQ in C#: A Complete Guide with Examples
Dec 09, 2023
How to sort a List in C#
Jun 01, 2023
Previous in C#
Create JSON String in C#
Next in C#
How to sort a List in C#

Comments

Contents

๐ŸŽฏ

Interview Prep

Ace your C# interview with curated Q&As for all levels.

View C# Interview Q&As

More in C#

  • Zoom C# Wrapper Integration 12896 views
  • Convert HTML String To Image In C# 11454 views
  • The report definition is not valid or is not supported by th… 10792 views
  • Replacing Accent Characters with Alphabet Characters in CSha… 9749 views
  • Get IP address using c# 8610 views
View all C# posts โ†’

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
  • SEO Analyzer
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ยท  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor