user2893761
user2893761

Reputation: 35

Select multiple row with same id

I have my "orders" table looks like:

orderId     CustomerName
1           customer1
2           customer2
3           customer1
4           customer1

and I have my code which only get one orderId

SqlCommand command = new SqlCommand("SELECT orderId FROM orders WHERE CustomerName='"+name+"'", connection);

SqlDataReader reader = command.ExecuteReader();

if (reader.Read()){
    MyOrders order1 = new MyOrders(reader.GetInt32(0));
    orders.Add(order1);
}
return orders.ToArray();

how to get all orderId 1,3,4 with customer1?

Upvotes: 0

Views: 671

Answers (1)

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48550

Use a loop to add all orders. Like this

while (reader.Read())
{
    MyOrders order1 = new MyOrders(reader.GetInt32(reader("orderId")));
    orders.Add(order1);
}

Upvotes: 2

Related Questions