What is ADO.NET?What are the key components of ADO.NET?What are the ADO.NET namespaces?What is the meaning of object pooling?What are the differences between ADO.NET and classic ADO?Explain the architecture of ADO.NET.?What is a Connection String in ADO.NET C#??How do you establish a database connection using ADO.NET?Explain the concept of Connection Pooling in ADO.NET C#.Differentiate between Object pooling and Connection pooling in C#?What is a DataReader in ADO.NET? Explain with C# example?What is the functionality of CommandBehavior.SchemaOnly?Why CommandBehavior.SingleResult flag is used in ADO.NET?What does CommandBehavior.SingleRow do in ADO.NET?How we can get multiple results by DataReader using same connection in C#?How can we force the connection object to close after my DataReader is closed?What is a DataSet in ADO.NET? Explain with C# example?What are typed and un-typed datasets in ADO.NET C#?Write down some of the characteristic of DataSet?What is the difference between dataSet and DataReader?Why is DataSet Slower than DataReader? Explain with Example.How does DataSet handle data in a disconnected environment?What is the Difference between connected and disconnected architectire?Explain HasChanges() method of DataSet in C#.Explain GetChanges() method with detaild C# Example.Explain RejectChanges() method with C# Example.Explain AcceptChanges() method with C# Example.What are the various methods provided by DataSet for XML in C#?What is the purpose of DataAdapter in ADO.NET?Explain the steps involved in retrieving data using DataAdapter.

DataReader in ADO.NET with C#

A DataReader in ADO.NET is a data retrieval component used in C# to efficiently read data from a database. It provides a forward-only, read-only access to the result set returned by a database query. The key advantage of using a DataReader is that it allows you to retrieve data from the database one row at a time, which can be very efficient for processing large result sets without loading the entire dataset into memory.

Here's a simple C# example of how to use a DataReader to fetch data from a SQL Server database and display it:


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

class Program
{
    static void Main()
    {
        string connectionString = "Server=your_server_name;Database=your_database_name;User Id=your_username;Password=your_password;";
        
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();

            string sqlQuery = "SELECT FirstName, LastName FROM Customers";
            using (SqlCommand command = new SqlCommand(sqlQuery, connection))
            {
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        string firstName = reader["FirstName"].ToString();
                        string lastName = reader["LastName"].ToString();
                        
                        Console.WriteLine($"First Name: {firstName}, Last Name: {lastName}");
                    }
                }
            }
        }
    }
}
    

Explanation of the code:

  1. We start by importing the necessary namespaces for ADO.NET components.
  2. We define a connection string, which contains the database server, database name, username, and password.
  3. Inside a using block, we create a SqlConnection object and open the database connection.
  4. We define a SQL query to select data from a "Customers" table.
  5. Next, we create a SqlCommand object with the SQL query and the database connection.
  6. Within another using block, we execute the command using ExecuteReader(), which returns a DataReader object.
  7. Inside a while loop, we use the Read() method of the DataReader to iterate through the result set row by row.
  8. We retrieve data from the DataReader using the column names (e.g., "FirstName" and "LastName") and display it in the console.
  9. The using blocks ensure that resources are properly disposed of when we're done with them.

Output


First Name: John, Last Name: Doe
First Name: Jane, Last Name: Smith
First Name: Bob, Last Name: Johnson

This code connects to a SQL Server database, fetches data from the "Customers" table, and displays the first name and last name of each customer. You can modify the SQL query and connection string to suit your specific database and table.