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

FoundationsSQL & safetyProductionProjects

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

  1. Insert and print Id.
  2. Verify in SSMS.
  3. Handle unique violations.
  4. Insert Status Shipped.
  5. 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.

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
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
Use Asynchronous Operations: For very large datasets, consider performing?
Short answer: database operations asynchronously to avoid blocking the main thread and keep the application responsive. Real-world example (ShopNest) For large order exports, ShopNest uses SqlDataReader (forward-only, fa…
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"…
Mid PDF Detailed
How do you perform asynchronous database operations in ADO.NET?
Short answer: ADO.NET supports asynchronous database operations using the async and await keywords in C#. Explain a bit more This allows the application to remain responsive while waiting for the database operation to co…
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