ADO.NET

ADO.NET is a data access technology in ASP.NET that allows to interact with databases. It provides a set of and libraries that enable the retrieval, manipulation, and storage of data from various data such as SQL Server, Oracle, and MySQL.

ADO.NET Components

ADO.NET consists of several key components:

  • Connection: Represents a connection to a data source. It provides methods to establish and manage the connection.
  • Command: Executes a SQL statement or stored procedure a data source. It can be used to retrieve data, modify data, or perform other operations.
  • DataReader: Provides a forward-only, read-only stream of data from a data source. It is used to efficiently retrieve large amounts of data.
  • DataAdapter: Acts as a bridge between a DataSet and a data source. It populates a DataSet with data from the data source and updates the data source with changes made in the DataSet.
  • DataSet: Represents an in- cache of data retrieved from a data source. It can hold multiple tables, relationships, and constraints.

Example:


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

namespace ADO.NETExample
{
     Program
    {
        static void Main(string[] args)
        {
            string connectionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=Username;Password=Password";
            
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                
                string sqlQuery = "SELECT * FROM Customers";
                
                using (SqlCommand command = new SqlCommand(sqlQuery, connection))
                {
                    using ( reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string customerName = reader["CustomerName"].ToString();
                            Console.WriteLine(customerName);
                        }
                    }
                }
            }
        }
    }
}

In this example, we are using ADO.NET to connect to a SQL Server database and retrieve data from the “Customers” table. We first establish a connection using the SqlConnection class and provide the necessary connection string. Then, we a SqlCommand object with the SQL query to be . The SqlDataReader class is used to read the data returned by the query. We iterate over the rows using the Read() method and retrieve the value of the “CustomerName” column for each row.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents