# Connecting Apps to SQL Server

Now your C# programs talk to the database. You will configure a project to connect to SQL Server, run queries with ADO.NET, keep user input safe with parameters, and map rows into C# objects.

Updated: November 3, 2026

## [Configure the Project for SQL Server](https://www.ask-codey.com/courses/csharp-full-stack-bootcamp/topics/configure-the-project-for-sql-server)

1. [Configure the Project for SQL Server](https://www.ask-codey.com/lessons/configure-the-project-for-sql-server) (Journeyman): Add the Microsoft.Data.SqlClient package to a C# project, write a connection string for your local SQL Server, and keep it in appsettings.json.

## [Querying a Database with ADO.NET](https://www.ask-codey.com/courses/csharp-full-stack-bootcamp/topics/querying-a-database-with-ado-net)

1. [Querying a Database with ADO.NET: an overview](https://www.ask-codey.com/lessons/querying-a-database-with-ado-net-an-overview) (Journeyman): Every database query from C# takes three steps, connect, run the SQL, read the results, and each step has its own ADO.NET object.
2. [ADO.NET Objects](https://www.ask-codey.com/lessons/querying-a-database-with-ado-net-ado-net-objects) (Journeyman): Use SqlConnection to connect, SqlCommand to run SQL, and SqlDataReader to read rows, choosing ExecuteReader, ExecuteScalar or ExecuteNonQuery for each kind of statement.
3. [Full Example](https://www.ask-codey.com/lessons/querying-a-database-with-ado-net-full-example) (Journeyman): A complete console program that reads the connection string from appsettings.json, queries Northwind's categories, and prints a report.

## [Working with User Input in C#](https://www.ask-codey.com/courses/csharp-full-stack-bootcamp/topics/working-with-user-input-in-csharp)

1. [Working with User Input](https://www.ask-codey.com/lessons/working-with-user-input-in-csharp) (Journeyman): Use what a user types in a query safely with SqlCommand parameters, and see why building SQL from their text opens the door to SQL injection.

## [Mapping Data to Models in C#](https://www.ask-codey.com/courses/csharp-full-stack-bootcamp/topics/mapping-data-to-models-in-csharp)

1. [Mapping Data to Models: an overview](https://www.ask-codey.com/lessons/mapping-data-to-models-in-csharp-an-overview) (Journeyman): Instead of using each row the moment it's read, copy it into a model object, so the program can keep and reuse the data after the reader is gone.
2. [Model Review](https://www.ask-codey.com/lessons/mapping-data-to-models-in-csharp-model-review) (Journeyman): Write a model class with one property for each column, choosing C# types that match the SQL types, and nullable types for columns that allow NULL.
3. [Mapping Rows to Models](https://www.ask-codey.com/lessons/mapping-data-to-models-in-csharp-mapping-rows-to-models) (Journeyman): Read each row of a query into a new model object, handle NULL columns, and collect the objects in a List&lt;T>.
4. [Data Access Objects](https://www.ask-codey.com/lessons/mapping-data-to-models-in-csharp-data-access-objects) (Journeyman): Gather all the SQL for one table into a Data Access Object (DAO), so the rest of the program asks for models and never sees the database.
5. [Example: a Product DAO](https://www.ask-codey.com/lessons/mapping-data-to-models-in-csharp-example-a-product-dao) (Journeyman): A complete ProductDao for Northwind, with methods to get, search, add, update and delete products, all with parameters and one mapping method.

## [Exercise: Connecting an App](https://www.ask-codey.com/courses/csharp-full-stack-bootcamp/topics/exercise-connecting-an-app)

1. [Exercise: Connecting an App](https://www.ask-codey.com/lessons/exercise-connecting-an-app) (Journeyman): Write the Data Access Objects for the BooksPlus bookstore (categories, books and customers) with ADO.NET, and run tests that check them against your own database.

Source: https://www.ask-codey.com/courses/csharp-full-stack-bootcamp/chapters/connecting-apps-to-sql-server
