2013-10-24 158 views
0

多行我有我的「訂單」表如下所示:選擇具有相同的ID

orderId  CustomerName 
1   customer1 
2   customer2 
3   customer1 
4   customer1 

,我有我的代碼只能獲得一個訂單ID

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(); 

如何讓所有的orderId 1, 3,4與customer1?

回答

2

使用循環添加所有訂單。像這樣

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