Mid ADO.NET

Using ExecuteReader (for a single row, multiple columns):?

SqlCommand command = new SqlCommand("SELECT CustomerName,

ContactName FROM Customers WHERE CustomerID = @CustomerID",

connection);

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

connection.Open();

SqlDataReader reader = command.ExecuteReader();

if (reader.Read()) // Checks if there's data

string customerName = reader["CustomerName"].ToString();

string contactName = reader["ContactName"].ToString();

Console.WriteLine($"Customer: {customerName}, Contact:

{contactName}");

Follow:

reader.Close();

connection.Close();

More from ADO.NET Core Tutorial

All questions for this course