1. ADO.NET Introduction

Introduction to ADO.NET

Overview

ADO.NET stands for ActiveX Data Objects .NET and is a technology for data access. It is a set of computer software components that programmers can use to access data and data services. It is a part of the base class library that is included with the Microsoft .NET Framework.

Key Points:

Data Providers

A data provider is a set of libraries that ADO.NET uses to interact with data sources. Data providers in ADO.NET include connection, command, data reader, and data adapter objects.

Example:

SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
    Console.WriteLine(String.Format("{0}, {1}", reader[0], reader[1]));
}

Object-Relational Mapping (ORM)

ORM is a technique that lets you interact with your database, like SQL Server, as if it were an object. ADO.NET also supports ORM.

Key Points:

Connection String

A connection string is a string that specifies information about a data source and the means of connecting to it. It is passed in code to an underlying driver or provider in order to initiate the connection.

Key Points:

SqlDataReader Class

The SqlDataReader class in ADO.NET is used for read-only, forward-only access to a SQL Server database.

Key Points:

Error Handling

If there is an error in connection.Open(), there is a problem in the connection string.

ExecuteReader, ExecuteNonQuery, and ExecuteScalar

Depending on the operation, the following methods are used:

SqlDataReader Methods

SqlParams

SqlParams is used to set values in SQL commands. It helps to prevent SQL injection attacks by parameterizing queries.

Example:

string query = "SELECT * FROM Customers WHERE City = @City";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@City", "London");
SqlDataReader reader = command.ExecuteReader();

This code selects all customers from London. The @City is a parameter, and its value is set by the AddWithValue method.

Reference

The content in this document is based on the original notes provided in Azerbaijani. For further details, you can refer to the original document using the following link:

Original Note - Azerbaijani Version