Tutorials ADO.NET Core Tutorial

Update Operations — Complete Guide

Update Operations — 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 13 of 100

Update Operations

FoundationsSQL & safetyProductionProjects

Foundations · 1 — Connections & CRUD · ~6 min · Module 2: CRUD Operations

What is this?

UPDATE changes existing rows; check rows affected to detect missing ids.

Why should you care?

ShopNest cancels and status changes are updates — must be parameterized and often transactional.

See it live — copy this example

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

cmd.CommandText = @"
UPDATE Orders SET Status = @Status
WHERE Id = @Id AND Status = @FromStatus;";
cmd.Parameters.Add("@Status", SqlDbType.NVarChar, 20).Value = "Cancelled";
cmd.Parameters.Add("@FromStatus", SqlDbType.NVarChar, 20).Value = "Pending";
cmd.Parameters.Add("@Id", SqlDbType.Int).Value = orderId;
var n = await cmd.ExecuteNonQueryAsync(ct);
if (n == 0) throw new InvalidOperationException("not pending");

What happened?

  • Optimistic guard with AND Status = @FromStatus.
  • Zero rows means conflict.

Practice next

  1. Cancel a pending order.
  2. Retry when n==0.
  3. Update Total carefully with checks.
  4. Try canceling Shipped — expect 0.
  5. Set UpdatedAt = SYSUTCDATETIME().

Remember

Guarded updates. Check rows affected. Parameters always.

ShopNest cancel order

Only Pending → Cancelled allowed.

Outcome: No accidental cancel of shipped orders.

Interview prep for this lesson

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

Mid PDF Detailed
Use a DataAdapter to update the database. Set the InsertCommand,?
Short answer: UpdateCommand, and DeleteCommand properties of the DataAdapter to define how data changes should be applied to the database. Real-world example (ShopNest) Always pass order ids with parameters: cmd.Paramete…
Mid PDF Detailed
Call the Update method on the DataAdapter to sync changes.?
Short answer: Example: // Assuming you already have a populated DataTable DataTable table = new DataTable(); SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Customers", connection); // Set commands f…
Mid PDF Detailed
It uses SQL commands to retrieve and update data.?
Short answer: Example: SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Customers", connection); DataSet dataset = new DataSet(); adapter.Fill(dataset, "Customers"); // Populates the DataSet…
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…
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