【发布时间】:2023-03-21 08:40:01
【问题描述】:
所以我正在做我的第一个项目,现在有些事情让我有点发疯,我一直在寻找,但似乎找不到答案。
我的数据库中有两张表,一张用于员工数据(employeedata),另一张只包含他们家中的图片(housepictures),只有三个字段(PhotoID、EmployeeID、Photo),使用外键员工数据表中的员工 ID。
我正在尝试从该表中检索图片并将它们放入各自的 PictureBox 中(总共有六张,因为我只为员工存储 6 张图片),但我只能设法检索第一张图片,或最后一张,或第一张图片并在每个 PictureBox 中重复它(同一张照片)。这是我当前的代码:
try
{
using (MySqlConnection conn = new MySqlConnection(connDB.connstring))
{
using (MySqlCommand cmd = new MySqlCommand("select HousePicture from Employees.housepictures where EmployeeID='" + id + "'", conn))
{
conn.Open();
using (MySqlDataReader dr = cmd.ExecuteReader())
{
if (dr.Read())
{
PictureBox[] pb = { pictureBoxHouse1, pictureBoxHouse2, pictureBoxHouse3, pictureBoxHouse4, pictureBoxHouse5, pictureBoxHouse6 };
for (int i = 0; i < pb.Length; i++)
{
using (MemoryStream stream = new MemoryStream())
{
if (dr["HousePicture"] != DBNull.Value)
{
byte[] image = (byte[])dr["HousePicture"];
stream.Write(image, 0, image.Length);
Bitmap bitmap = new Bitmap(stream);
pb[i].Image = bitmap;
}
}
}
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
非常感谢任何建议!
【问题讨论】: