Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • 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
  • Register
  • Login
  1. Home
  2. Blogpost

DataReader in ADO.NET

Date- May 31,2023

4841

DataReader in ADONET

In ADO.NET, the DataReader class is used to retrieve data in a forward-only, read-only manner from a data source. It provides a fast and efficient way to access data when you don't need to update or modify the data.

Here's an example of how you can use the DataReader class in C#:

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())
                {
                    // Access data from the reader using the column index or column name
                    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 first create a SqlConnection object with the appropriate connection string. Then, we create a SqlCommand object with the SQL query we want to execute.

Inside the using statement, we open the connection and execute the query using the ExecuteReader() method, which returns a DataReader object.

We then use a while loop to iterate over the rows returned by the DataReader. The Read() method advances the reader to the next row. Inside the loop, we can access the values in each column of the current row using the appropriate Get methods provided by the DataReader (e.g., GetInt32, GetString, etc.), either by column index or column name.

Finally, we output the retrieved data to the console.

Remember to replace "YourConnectionString" with the actual connection string for your database. Also, make sure to handle any exceptions that may occur during the execution of the code.

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Comments

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 Join Us On Facebook
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
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
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
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page