ADO.NET: Basics of Database Connectivity in C#
Table of Contents
- Introduction
- What is ADO.NET?
- Core Components of ADO.NET
- Connecting to a Database
- Executing SQL Commands
- Reading Data with DataReader
- Working with DataSet and DataAdapter
- Best Practices for ADO.NET
- Conclusion
Introduction
When building applications that interact with databases, developers need a powerful, reliable way to connect, query, and manage data. In the world of .NET, ADO.NET remains one of the most essential frameworks for database connectivity. Even with modern ORMs like Entity Framework, understanding ADO.NET gives you deeper control, performance benefits, and insights into how data flows underneath higher-level abstractions.
What is ADO.NET?
ADO.NET (ActiveX Data Objects for .NET) is a data access technology used to communicate with databases like SQL Server, MySQL, Oracle, and more. It provides the fundamental building blocks required to:
- Connect to a database
- Execute SQL commands
- Retrieve data
- Update records
- Manage disconnected data
Core Components of ADO.NET
- SqlConnection – Manages database connection.
- SqlCommand – Executes SQL statements.
- SqlDataReader – Fast, forward-only data retrieval.
- DataSet & DataTable – In-memory data representation.
- SqlDataAdapter – Bridges DataSet and database.
Connecting to a Database
Below is the simplest way to create a connection using SqlConnection:
using System.Data.SqlClient;
string connectionString = "Server=.;Database=SampleDB;Trusted_Connection=True;";
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
Console.WriteLine("Database connected successfully!");
}
Executing SQL Commands
Use SqlCommand to execute a SQL query:
string query = "INSERT INTO Employees (Name, Age) VALUES (@Name, @Age)";
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("@Name", "Gokul");
cmd.Parameters.AddWithValue("@Age", 25);
int rows = cmd.ExecuteNonQuery();
Console.WriteLine($"{rows} record inserted.");
}
Reading Data with SqlDataReader
string query = "SELECT Id, Name, Age FROM Employees";
using (SqlCommand cmd = new SqlCommand(query, conn))
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine($"{reader["Id"]}, {reader["Name"]}, {reader["Age"]}");
}
}
Working With DataSet and DataAdapter
Disconnected data access:
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Employees", conn);
adapter.Fill(ds, "Employees");
foreach (DataRow row in ds.Tables["Employees"].Rows)
{
Console.WriteLine(row["Name"]);
}
Best Practices
- Always use
usingblocks for connections. - Use parameterized queries to prevent SQL injection.
- Keep database operations short and optimized.
- Prefer DataReader for performance-heavy reads.
Conclusion
ADO.NET remains one of the most powerful tools for C# developers working directly with SQL databases. From executing simple commands to managing disconnected data, it provides complete control over database interactions. Whether you're building enterprise software or learning low-level data access, mastering ADO.NET is a valuable skill.
This Content Sponsored by SBO Digital Marketing. Mobile-Based Part-Time Job Opportunity by SBO! Earn money online by doing simple content publishing and sharing tasks. Here's how: Job Type: Mobile-based part-time work Work Involves: Content publishing Content sharing on social media Time Required: As little as 1 hour a day Earnings: ₹300 or more daily Requirements: Active Facebook and Instagram account Basic knowledge of using mobile and social media For more details: WhatsApp your Name and Qualification to 9025032394 a.Online Part Time Jobs from Home b.Work from Home Jobs Without Investment c.Freelance Jobs Online for Students d.Mobile Based Online Jobs e.Daily Payment Online Jobs Keyword & Tag: #OnlinePartTimeJob #WorkFromHome #EarnMoneyOnline #PartTimeJob #jobs #jobalerts #withoutinvestmentjob


0 Comments