What is ADO.NET?
ADO.NET is the core data access technology for .NET. It provides a rich set of components for connecting to databases, executing commands, and retrieving results. ADO.NET works with relational databases (SQL Server, MySQL, PostgreSQL) and non‑relational stores (Azure Cosmos DB, ODBC).
Key Concepts
- Connection – Represents a session with a data source.
- Command – Executes SQL statements or stored procedures.
- DataReader – Provides fast, forward‑only, read‑only access to query results.
- DataAdapter & DataSet – Enables disconnected data manipulation.
- Parameterization – Prevents SQL injection and improves performance.
Getting Started
Below is a minimal example that connects to a SQL Server database, runs a query, and prints the results.
using System;
using System.Data.SqlClient;
class Program
{
static void Main()
{
string connStr = "Server=localhost;Database=Demo;Integrated Security=true;";
using (var connection = new SqlConnection(connStr))
{
connection.Open();
string sql = "SELECT Id, Name FROM Customers;";
using (var command = new SqlCommand(sql, connection))
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine($"{reader["Id"]}: {reader["Name"]}");
}
}
}
}
}
Further Reading
Explore the following topics to deepen your knowledge: