Home / Learn Secure SQL / C#: ADO.NET SqlCommand Parameters

C#: ADO.NET SqlCommand Parameters

Parameters.Add / AddWithValue on a SqlCommand.

Bottom lineParameters.Add / AddWithValue on a SqlCommand.

Breaking it down

ADO.NET patterncommand.CommandTextwith @namecommand.Parameters.AddWithValue("@email",…command.ExecuteReader()
ADO.NET pattern

What it is

ADO.NET's SqlCommand (and the equivalents for other databases) takes @-prefixed placeholders in CommandText, filled via the Parameters collection.

Why it matters

Dapper and Entity Framework (covered in the ORM part) build on this same underlying mechanism, so understanding raw ADO.NET parameters clarifies what those higher-level tools are doing for you.

Mechanically speaking

Set CommandText with @name placeholders, then add each parameter with its name and value via Parameters.Add or AddWithValue.

Shows up in

  • Web application code review
  • Backend development
  • Database administration
C# ADO.NET
using var command = connection.CreateCommand();
command.CommandText = "SELECT * FROM users WHERE email = @email";
command.Parameters.AddWithValue("@email", email);
using var reader = command.ExecuteReader();
while (reader.Read()) { /* read columns */ }

Check yourself

1. In ADO.NET, placeholders are typically written as...