Mid
ADO.NET
ADO.NET
How would you retrieve a single row or column of data using
DO.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();
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png