如何从数据库中检索图像并将其显示在图片框中 [英] how to retrieve images from database and displaying it in picturebox

查看:84
本文介绍了如何从数据库中检索图像并将其显示在图片框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有Access数据库。我在访问数据库中插入了员工数据和图像。当我点击Datagridview Click事件数据正在更新到文本框。用同样的点击照片即更新到图片框。但我无法在图片框中显示图像。请帮帮我。



我将图像转换成字节并存储在数据库中。喜欢

Hi,

I have Access Database. I inserted Employee Data and images in access Database. When i clicked on Datagridview Click Event data is updating to textboxes. With the same click Photo is to update to picturebox. But i am not able to display image in picture box. Please help me.

I converted image into bytes and stored in Database. like

MessageBox.Show("Saving Data at Index :" + rowPosition.ToString());

              OleDbCommand oledbinsert = new OleDbCommand("Insert INTO EmployeeData(EmpID,EmployeeName,PassportNo,PassportExpiry,AddressMalaysia,



OleDbParameter imageParameter = oledbinsert.Parameters.AddWithValue(@ Img,SqlDbType.Binary);

imageParameter .Value = ImageAsBytes;

imageParameter.Size = ImageAsBytes.Length;



int rowsAffected = oledbinsert.ExecuteNonQuery();

MessageBox.Show(数据存储成功+ rowsAffected.ToString()+Row);

MessageBox.Show(数据成功添加到员工数据);


OleDbParameter imageParameter = oledbinsert.Parameters.AddWithValue("@Img", SqlDbType.Binary);
imageParameter.Value = ImageAsBytes;
imageParameter.Size = ImageAsBytes.Length;

int rowsAffected = oledbinsert.ExecuteNonQuery();
MessageBox.Show("Data Stored Successfully in" + rowsAffected.ToString() + "Row");
MessageBox.Show("Data Successfully Added to Employee Data");

推荐答案

Your Solution Is very simple







void RetriveImage()
{
    OleDbConnection conn = new OleDbConnection(@"Your Connection String");
    conn.Open();
    using (conn)
    {

        Image img ;


        byte[] photo ;

        OleDbCommand cmd = new OleDbCommand("SELECT Pic From ATEST where ID=4", conn);
        OleDbDataAdapter reader;
        reader = cmd.ExecuteReader();
        reader.Read();
        photo =(byte []) reader.GetValue(0);

        MemoryStream ms = new MemoryStream(photo);

        img = Image.FromStream(ms);

        pictureBox1.Image = img;




    }
    conn.Close();

}


谢谢兄弟。但它无法正常工作。

只需查看gridview点击事件中的代码:



Thank You Bro. But its not working.
Just Look at my code in gridview click event :

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                try
                {

                    DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
                    Txt_EmpID.Text = row.Cells[0].Value.ToString();
                    Txt_EmpName.Text = row.Cells[1].Value.ToString();
                    Txt_Passport.Text = row.Cells[2].Value.ToString();
                    Dtp_PpExpiry.Text = row.Cells[3].Value.ToString();
                    txt_Address1.Text = row.Cells[4].Value.ToString();
                    Txt_Address2.Text = row.Cells[5].Value.ToString();
                    dtpDOB.Text = row.Cells[6].Value.ToString();
                    Txt_Emergency.Text = row.Cells[7].Value.ToString();
                    Txt_Emerg2.Text = row.Cells[8].Value.ToString();
                    Txt_Bank.Text = row.Cells[9].Value.ToString();
                    Txt_Dl.Text = row.Cells[10].Value.ToString()
    MessageBox.Show("Loading Data");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error" + ex);
                }
               // pictureBox1.Image = row.Cells[18].Value as Image;
                //pictureBox1.Image = dataGridView1.CurrentRow.Cells[18].Value as Image;

               // LoadImage();
            }
            RetriveImage();
        }<pre lang="cs">

 <pre lang="cs">void RetriveImage()
        {
            OleDbConnection DBConnection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Administrator\Desktop\EmployeeData.accdb");
            DBConnection.Open();
            using (DBConnection)
            {

                Image img;


                byte[] photo;

                OleDbCommand cmd = new OleDbCommand("SELECT img From EmployeeData where EmpID=4", DBConnection);
                //OleDbDataAdapter reader = new OleDbDataAdapter();
              //  OleDbDataReader reader;
                OleDbDataReader reader = cmd.ExecuteReader();
               // reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    photo = (byte[])reader.GetValue(0);

                    MemoryStream ms = new MemoryStream(photo);

                    img = Image.FromStream(ms);


                    pictureBox1.Image = img;
                }
                

            }
            DBConnection.Close();

        }
<pre lang="cs">


这篇关于如何从数据库中检索图像并将其显示在图片框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆