Mid ADO.NET

How would you retrieve a single row or column of data using ADO.NET?

To retrieve a single row or column of data, you can use a SqlCommand and either execute

it with ExecuteScalar (for single column data) or ExecuteReader (for a single row).

Using ExecuteScalar (for a single column value, like an aggregate):

SqlCommand command = new SqlCommand("SELECT CustomerName FROM

Customers WHERE CustomerID = @CustomerID", connection);

command.Parameters.AddWithValue("@CustomerID", 1);

connection.Open();

string customerName = (string)command.ExecuteScalar();

Console.WriteLine("Customer Name: " + customerName);

connection.Close();

More from ADO.NET Core Tutorial

All questions for this course