C# 从access数据库将图像检索到picturebox
我正在尝试使用listbox选择事件从access数据库检索图像 我使用以下代码将数据保存到数据库:C# 从access数据库将图像检索到picturebox,c#,database,C#,Database,我正在尝试使用listbox选择事件从access数据库检索图像 我使用以下代码将数据保存到数据库: command.CommandText = "insert into EmployeeInfo (FirstName,LastName,Pay,Pic) values ('" + txt_fname.Text + "' , '" + txt_lname.Text + "' , '" + txt_pay.Text + "' , @productpic)"; MemorySt
command.CommandText = "insert into EmployeeInfo (FirstName,LastName,Pay,Pic) values ('" + txt_fname.Text + "' , '" + txt_lname.Text + "' , '" + txt_pay.Text + "' , @productpic)";
MemoryStream stream = new MemoryStream();
pb1.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] pic = stream.ToArray();
command.Parameters.AddWithValue("@productpic", pic);
我正在使用以下代码检索数据:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "select * from EmployeeInfo where FirstName = '" + listBox1.Text +"'";
command.CommandText = query;
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read()) {
list_fname.Text = reader["FirstName"].ToString();
list_lname.Text = reader["LastName"].ToString();
list_dob.Text = reader["DoB"].ToString();
}
connection.Close();
}
现在我要做的是检索与每一行相关联的图片,字段名是Pic到picturebox中,并在while循环中与其他数据一起显示。我看到了其他人提出的几个问题,但每个人都在使用datagridview检索数据,在我的例子中,我试图在listbox select事件中检索数据
任何形式的帮助都将不胜感激。提前谢谢 在列表框选择事件的while循环中调用此函数:
void loadpicture()
{
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "select pic from EmployeeInfo where FirstName = '" + listBox1.Text + "'";
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
byte[] content = (byte[])ds.Tables[0].Rows[0].ItemArray[0];
MemoryStream stream = new MemoryStream(content);
pb1.Image = Image.FromStream(stream);
}
参考这个,我已经想出了解决办法。谢谢。然后请在此处添加您的解决方案作为答案,以便其他用户稍后可以找到。已添加解决方案。谢谢