adonet
  1. adonet-datareader

DataReader - (ADO.NET Connection)

DataReader is a part of the ADO.NET framework in .NET that allows you to retrieve data from a database server in a read-only, forward-only manner. It provides a fast and efficient way to read data from the database without loading the entire database into memory, which is especially useful when working with large sets of data.

Syntax

The syntax for using a DataReader is as follows:

using System.Data.SqlClient;

var connectionString = "Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;";

using var connection = new SqlConnection(connectionString);
using var command = new SqlCommand("SELECT * FROM myTable", connection);

connection.Open();

using var reader = command.ExecuteReader();

while (reader.Read())
{
    // Read the data from the reader
}

reader.Close();

Example

Here's a simple example that retrieves data from a SQL Server database using a DataReader:

using System.Data.SqlClient;

var connectionString = "Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;";

using var connection = new SqlConnection(connectionString);
using var command = new SqlCommand("SELECT * FROM Customers", connection);

connection.Open();

using var reader = command.ExecuteReader();

while (reader.Read())
{
    Console.WriteLine($"{reader["CustomerID"]} {reader["ContactName"]}");
}

reader.Close();

Output

The output of the above code will be a list of customer IDs and contact names retrieved from the database.

ALFKI Maria Anders
ANATR Ana Trujillo
ANTON Antonio Moreno
...

Explanation

A DataReader is a lightweight object that provides a way to read data from a database in a forward-only, read-only manner. It has a lower memory footprint than other data access objects like DataSets and allows you to retrieve large amounts of data efficiently.

To use a DataReader, you first create a connection to the database using a SqlConnection object. Then, you create a command object that specifies the SQL statement to execute. Finally, you execute the command using the ExecuteReader() method which returns a SqlDataReader object. You can then use the DataReader to retrieve the data row-by-row using the Read() method and access the values using the indexer [] or the GetXXX() methods.

Use

You can use a DataReader to retrieve data from a variety of data sources such as SQL Server, Oracle, MySQL, and more. It is useful when you need to retrieve large amounts of data efficiently and when you only need to read the data once.

Important Points

  • A DataReader provides a way to read data from a database in a forward-only, read-only manner.
  • You can use a DataReader to retrieve large amounts of data efficiently.
  • A DataReader has a lower memory footprint than other data access objects like DataSets.
  • A DataReader is useful when you only need to read the data once.

Summary

In this page, we discussed the basics of DataReader in ADO.NET connection. We covered the syntax, example, output, explanation, use, and important points of DataReader. A DataReader is a fast and efficient way to read data from a database in a forward-only, read-only manner. Its low memory footprint and efficiency make it perfect for retrieving large amounts of data from a database with minimal overhead.

Published on: