繁体   English   中英

从Access数据库将图像检索到图片框

[英]Retrieve Image to a picturebox from access database

我正在尝试使用列表框选择事件从Access数据库检索图像

我使用以下代码将数据保存到数据库:

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进入图片框,并与其他数据一起在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);

            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM