Tutorials ADO.NET Core Tutorial
Insert Operations — Complete Guide
Insert 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 11 of 100
Insert Operations
Foundations → SQL & safety → Production → Projects
Foundations · 1 — Connections & CRUD · ~6 min · Module 2: CRUD Operations
What is this?
INSERT adds rows; ExecuteNonQueryAsync returns rows affected. OUTPUT can return new ids.
Why should you care?
Every new ShopNest order starts as an insert.
See it live — copy this example
Use a .NET console or API project with SQL Server LocalDB. Run dotnet run after pasting.
cmd.CommandText = @"
INSERT INTO Orders (CustomerId, Total, Status)
OUTPUT INSERTED.Id
VALUES (@CustomerId, @Total, @Status);";
cmd.Parameters.Add("@CustomerId", SqlDbType.Int).Value = 7;
cmd.Parameters.Add("@Total", SqlDbType.Decimal).Value = 999m;
cmd.Parameters.Add("@Status", SqlDbType.NVarChar, 20).Value = "Pending";
var newId = (int)(await cmd.ExecuteScalarAsync(ct))!;
Console.WriteLine(newId);
What happened?
- OUTPUT INSERTED.Id returns identity in one round trip.
- Use a transaction when inserting related tables.
Practice next
- Insert and print Id.
- Verify in SSMS.
- Handle unique violations.
- Insert Status Shipped.
- Catch SqlException 2627.
Remember
INSERT + parameters. OUTPUT for ids. Tx for related rows.
ShopNest place-order insert
API returns new order id.
Outcome: Client can poll status by id.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!