我有一个带有主人桌和宠物桌的小型VET应用程序,每个主人有4只宠物。
我设法编写了连接语句:
string getOwner = "SELECT Owner.name AS owner_name, Pet.name AS pet_name FROM Owner JOIN Pet ON owner_id = f_owner_id ";
现在我正在通过阅读器循环
SqlCommand select = new SqlCommand(getOwner, conn);
int i = 0;
SqlDataReader reader = select.ExecuteReader();
while (reader.Read())
{
Owner owner = new Owner(reader[0].ToString());
owner.pets = new Pet[4];
owner.pets[0] = new Pet(reader["pet_name"].ToString());
owner.pets[1] = new Pet(reader.GetValue(1).ToString());
owner.pets[2] = new Pet(reader["pet_name"].ToString());
owner.pets[3] = new Pet(reader["pet_name"].ToString());
try
{
owners[i] = owner;
}
catch { }
i++;
}
但它只给了我加入的第一场比赛,这意味着我只得到每个主人的第一个宠物,但我无法访问第2-4个宠物。读取器数组的大小只有2,第一个是所有者名称,第二个是第一个宠物的名称。
我怎样才能访问其他宠物? 我还没有找到答案,我真的很感激提示;)