CHAPTER 25
Beginner
Database Programming with ADO.NET
Updated: May 17, 2026
5 min read
# CHAPTER 25
Database Programming with ADO.NET
1. Introduction
While file handling (Chapter 22) is great for simple notes, enterprise applications require robust databases to manage thousands of users, transactions, and relational data. ADO.NET (ActiveX Data Objects for .NET) is the core Microsoft framework that allows your C# code to talk directly to databases like Microsoft SQL Server.2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the Connection String.
-
Use
SqlConnectionto connect to a database.
-
Execute SQL queries using
SqlCommand.
-
Read database rows using
SqlDataReader.
- Prevent SQL Injection using Parameters.
3. Required NuGet Package
In modern .NET, you must install the SQL Client package before coding. Open your terminal and run:dotnet add package Microsoft.Data.SqlClient
4. The Connection String
A connection string is a secure URL that tells C# exactly where the database is, what it's named, and how to log in.
csharp
5. Connecting and Reading Data
We use three main classes:SqlConnection (opens the door), SqlCommand (sends the SQL query), and SqlDataReader (reads the response).
csharp
6. Inserting Data (Executing Non-Queries)
If your SQL statement does not return rows (e.g.,INSERT, UPDATE, DELETE), you use ExecuteNonQuery() instead of ExecuteReader().
csharp
7. Preventing SQL Injection (CRITICAL)
Never concatenate user input directly into a SQL string:"INSERT INTO Students VALUES ('" + userInput + "')"
If the user types 'DROP TABLE Students;--, they will delete your entire database!
The Solution: Parameterized Queries.
csharp
8. Common Mistakes
-
Leaving Connections Open: If you forget to call
conn.Close()(or forget theusingblock), you will run out of connections in the SQL Server Connection Pool, and your entire application will crash.
-
SQL Injection Vulnerabilities: Failing to use
cmd.Parameters.AddWithValueis the #1 cause of database hacks.
9. Best Practices
-
Never hardcode the connection string inside
Program.cs. In real apps, store it securely in anappsettings.jsonfile or Environment Variables.
-
Always wrap ADO.NET objects (
SqlConnection,SqlCommand,SqlDataReader) inusingblocks.
10. Exercises
-
1.
Write a C# program that connects to a local SQL database and runs an
UPDATEquery to change a student's age based on their ID. Use parameterized queries.
11. MCQs with Answers
Q1. What does ADO.NET do? a) Compiles C# code b) Provides classes for connecting C# applications to databases c) Handles UI rendering Answer: b) Provides classes for connecting C# applications to databases
Question 2
Which package is required to connect to SQL Server in modern .NET?
Question 3
What information is held in a Connection String?
Question 4
Which class establishes the actual link to the database?
Question 5
Which method executes a SELECT statement and returns rows?
Question 6
Which method executes an INSERT, UPDATE, or DELETE statement?
Question 7
What does ExecuteNonQuery() return?
Question 8
What is SQL Injection?
Question 9
How do you prevent SQL Injection in ADO.NET?
Question 10
Why must SqlConnection be placed inside a using block?
12. Interview Questions
-
Q: Differentiate between
ExecuteReader(),ExecuteNonQuery(), andExecuteScalar().
- Q: Explain what Connection Pooling is and why it matters in ADO.NET.
- Q: Demonstrate how to write a SQL query in C# that is 100% immune to SQL injection.
13. Summary
ADO.NET provides the raw, low-level classes (SqlConnection, SqlCommand, SqlDataReader) needed to execute SQL commands from C#. While extremely fast and powerful, it requires careful memory management (using blocks) and security vigilance (Parameterized Queries) to prevent SQL Injection.