ADO.NET · Practice

Interview Questions

16 questions you are likely to be asked about ADO.NET, from beginner to experienced level — each with a spoken-style answer, a common follow-up and a link to the lesson behind it.

Advanced 15 min Topic 32 of 32
0/32 in ADO.NET

Beginner questions (5)

01What is ADO.NET?

The data access technology built into .NET. Providers such as Microsoft.Data.SqlClient supply connections, commands, data readers and adapters, so C# code can run SQL and stored procedures and read the results. EF Core and Dapper are built on top of it.

Learn: Introduction to ADO.NET
02What is the difference between the connected and disconnected models?

Connected: open a SqlConnection, run a SqlCommand and stream rows with a SqlDataReader while the connection stays open. Disconnected: a SqlDataAdapter fills a DataSet/DataTable, closes the connection, and you work with the in-memory copy, later pushing changes back with Update.

Learn: ADO.NET Architecture
03What do ExecuteNonQuery, ExecuteScalar and ExecuteReader return?

ExecuteNonQuery returns the number of rows affected (for INSERT/UPDATE/DELETE). ExecuteScalar returns the first column of the first row (or null if there are no rows). ExecuteReader returns a SqlDataReader for streaming result rows.

Learn: SqlCommand
04Why should you always use parameters?

Parameters send values separately from the SQL text, so input can never change the query (preventing SQL injection). They also handle types, dates and quotes correctly and let SQL Server reuse execution plans.

C# Illustrative
using var cmd = new SqlCommand("SELECT Id, Name FROM Products WHERE Name LIKE @q", conn);
cmd.Parameters.Add("@q", SqlDbType.NVarChar, 100).Value = 
quot;%
{search}%";
Learn: Parameterized Queries
05Why wrap connections in using?

using guarantees Dispose (which closes the connection) runs even if an exception is thrown, returning the physical connection to the pool. Leaked connections exhaust the pool and cause timeout errors under load.

Learn: SqlConnection

Intermediate questions (6)

01How does connection pooling work?

The provider keeps open physical connections in a pool per unique connection string (and identity). Open takes one from the pool; Close/Dispose returns it. The default maximum is 100 connections. Open late, close early, and keep connection strings identical.

Learn: Connection Pooling
02How do you call a stored procedure with an output parameter?

Set CommandType.StoredProcedure, add input parameters, add an output parameter with Direction = ParameterDirection.Output (and a size for strings), execute, then read parameter.Value. Use ParameterDirection.ReturnValue for the RETURN code.

Learn: Output Parameters and Return Values
03How do you handle NULL from the database?

Database NULL arrives as DBNull.Value, not null. Check reader.IsDBNull(ordinal) before calling typed getters, map to nullable C# types, and send (object?)value ?? DBNull.Value when setting parameter values.

Learn: Handling NULL Values
04How do you use a transaction in ADO.NET?

Call connection.BeginTransaction(), assign the transaction to every command, call Commit when all succeed and Rollback (or simply dispose without committing) on failure. Keep transactions short to avoid blocking.

C# Illustrative
using var tx = conn.BeginTransaction();
debit.Transaction = tx; credit.Transaction = tx;
await debit.ExecuteNonQueryAsync();
await credit.ExecuteNonQueryAsync();
tx.Commit();
Learn: Transactions
05Why use async ADO.NET methods in web apps?

OpenAsync, ExecuteReaderAsync and ReadAsync free the thread while waiting for SQL Server, so the server handles more concurrent requests with fewer threads. Pass a CancellationToken so abandoned requests stop their queries.

Learn: Async Data Access
06What is the difference between DataSet and DataTable?

A DataTable is one in-memory table of rows and columns. A DataSet is a container of several DataTables plus DataRelations between them. Both track row state (Added, Modified, Deleted) for adapter updates.

Learn: DataSet and DataTable

Experienced questions (5)

01How do you insert 100,000 rows quickly?

Use SqlBulkCopy with a DataTable or IDataReader source, explicit ColumnMappings, a sensible BatchSize and optionally TableLock. It uses the bulk insert protocol and is orders of magnitude faster than row-by-row INSERTs.

Learn: SqlBulkCopy
02What are table-valued parameters?

A user-defined table type in SQL Server lets you pass a whole set of rows as one parameter (SqlDbType.Structured with TypeName). Ideal for “WHERE Id IN (list)” or batch inserts without building dynamic SQL.

Learn: Table-Valued Parameters
03Which SqlException numbers do you commonly handle?

2627/2601 duplicate key, 547 foreign key or check constraint violation, 1205 deadlock victim (retry), -2 client timeout. Map them to meaningful results such as HTTP 409 instead of a generic 500.

Learn: Error Handling
04When would you choose ADO.NET or Dapper over EF Core?

For performance-critical or complex hand-tuned SQL, reporting, bulk operations and stored-procedure-heavy databases. EF Core is more productive for typical CRUD with change tracking and migrations. Many apps mix them.

Learn: ADO.NET vs Entity Framework
05How do you structure ADO.NET code in an ASP.NET Core app?

Read the connection string from configuration, register a small connection factory and repositories in DI, keep SQL in repositories behind interfaces, use async methods and return DTOs or domain objects — never SqlDataReader — to callers.

Learn: Repository Pattern
Progress is saved in this browser. Sign in to sync.