Mid From PDF ADO.NET 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}");

}

reader.Close();

connection.Close();

More from ADO.NET Core Tutorial

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details