Tutorials ADO.NET Core Tutorial

Connection Strings — Complete Guide

Connection Strings — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of ADO.NET Core Tutorial on Toolliyo Academy.

On this page

ADO.NET Core Tutorial · Lesson 5 of 100

Connection Strings

FoundationsSQL & safetyProductionProjects

Foundations · 1 — Connections & CRUD · ~6 min · Module 1: ADO.NET Fundamentals

What is this?

A connection string names server, database, auth, and pool options that SqlConnection uses.

Why should you care?

Wrong strings are the #1 “works on my machine” failure for ShopNest deploys.

See it live — copy this example

Use a .NET console or API project with SQL Server LocalDB. Run dotnet run after pasting.

// appsettings.json
// "ConnectionStrings": { "ShopNestDb": "Server=(localdb)\mssqllocaldb;Database=ShopNestDb;Trusted_Connection=True;TrustServerCertificate=True;Encrypt=True" }
var cs = config.GetConnectionString("ShopNestDb");
Console.WriteLine(cs!.Contains("ShopNestDb"));

What happened?

  • Keep secrets out of source.
  • Prefer config + User Secrets/Key Vault.
  • Encrypt=True for modern clients.

Practice next

  1. Put the string in appsettings.Development.json.
  2. Read via IConfiguration.
  3. Never commit passwords.
  4. Add Max Pool Size=50.
  5. Switch to Docker host name.

Remember

Config, not constants. Encrypt when possible. One name: ShopNestDb.

ShopNest config wiring

API reads ShopNestDb from configuration.

Outcome: Staging/prod only swap secrets.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Mid PDF Detailed
Bind the data to the GridView. Example: protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { SqlConnection connection = new SqlConnection(connectionString); SqlDataAdapter adapter = new SqlDataAdapter("SELECT CustomerID, CustomerName FROM Customers", connection); DataTable dataTable = new DataTable();
Short answer: dapter.Fill(dataTable); GridView1.DataSource = dataTable; GridView1.DataBind(); } } Here, GridView1 is bound to the data returned from the SQL query (SELECT CustomerID, CustomerName FROM Customers), and the…
Mid PDF Detailed
It uses SQL commands to retrieve and update data. Example: SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Customers", connection); DataSet dataset = new DataSet();
Short answer: dapter.Fill(dataset, "Customers"); // Populates the DataSet with data from the "Customers" table dapter.Fill(dataset, "Customers"); // Populates the DataSet with data from the…
Mid PDF Detailed
Use the DataAdapter's Update() method to push the changes back to the database. Example: SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Customers", connection); SqlCommandBuilder commandBuilder = new SqlCommandBuilder(adapter); // Automatically generates insert, update, delete commands DataSet dataset = new DataSet();
Short answer: dapter.Fill(dataset, "Customers"); // Modify data in the DataSet dataset.Tables["Customers"].Rows[0]["CustomerName"] = "New Name"; // Update the database with the mod…
Mid PDF Detailed
Closing the connection with the Close() method to release resources.?
Short answer: Example: using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); // Execute commands here connection.Close(); } Example code Example: using (SqlConnection connection = ne…
Mid PDF Detailed
Call the Update method on the DataAdapter to sync changes. Example: // Assuming you already have a populated DataTable DataTable table = new DataTable(); SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Customers", connection); // Set commands for Insert, Update, and Delete
Short answer: dapter.UpdateCommand = new SqlCommand("UPDATE Customers SET CustomerName = @CustomerName WHERE CustomerID = @CustomerID", connection); dapter.UpdateCommand.Parameters.Add("@CustomerName"…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

ADO.NET Core Tutorial
Course syllabus

ADO.NET Core Tutorial

Module 1: ADO.NET Fundamentals
Module 2: CRUD Operations
Module 3: Stored Procedures
Module 4: Transactions and Error Handling
Module 5: Performance Optimization
Module 6: ASP.NET Core Integration
Module 7: Advanced Enterprise Topics
Module 8: Testing and Debugging
Module 9: Cloud and DevOps
Module 10: Real-World Enterprise Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details