4
Reply

inserting and retrieving image in MS Access 2013

Ask a question
Ajay Kadian

Ajay Kadian

10y
1.1k
1
I want insert and retrieve the image in ms access 2013.

And I am using this query in ms access 2013
create table pkd(EID int,First_Name char(10),Last_Name char(10),Picture image ).
So question is that when i insert the image it will inserted but when i retrieve its says record not found.

using System.Data.OleDb;
using System.IO;

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        OleDbConnection cn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\\Msaccess.accdb");
        OleDbCommand command;
        string imgLoc = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "jpeg|*.jpg|bmp|*.bmp|all files|*.*";
                dlg.Title = "Select Employee Picture";
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    imgLoc = dlg.FileName.ToString();
                    pictureBox1.ImageLocation = imgLoc;

                }

            }
            catch (Exception)
            {
                MessageBox.Show("Please enter the valid image.");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                byte[] img = null;
                FileStream fs = new FileStream(imgLoc, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fs);
                img = br.ReadBytes((int)fs.Length);

                string sql = "INSERT INTO pkd(EID,FIRST_NAME,LAST_NAME,PICTURE)VALUES('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "',@img)"; // string values 
                if (cn.State != ConnectionState.Open)
                    cn.Open();
                command = new OleDbCommand(sql, cn);
                command.Parameters.Add(new OleDbParameter("@img", img));
                command.ExecuteNonQuery();
                cn.Close();
                MessageBox.Show("Record(s) Saved.");

            }
            catch (Exception)
            {
                cn.Close();
                MessageBox.Show("Insertion Failed");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string sql = "SELECT FIRST_NAME,LAST_NAME,PICTURE FROM pkd WHERE EId='" + textBox1.Text + "'";
                cn.Open();
                command = new OleDbCommand(sql, cn);
                OleDbDataReader reader = command.ExecuteReader();
                reader.Read();
                if (reader.HasRows)
                {
                    textBox2.Text = reader[0].ToString();
                    textBox3.Text = reader[1].ToString();
                    byte[] img = (byte[])(reader[2]);
                    if (img == null)
                        pictureBox1.Image = null;
                    else
                    {
                        MemoryStream ms = new MemoryStream(img);
                        pictureBox1.Image = Image.FromStream(ms);
                        MessageBox.Show("Search SuccessFul.");
                    }
                }
                else
                {
                    MessageBox.Show("Id Doesnot exists");
                }
                cn.Close();
            }
            catch (Exception)
            {
                cn.Close();
                MessageBox.Show("Id Does not Exists.");
            }
        }
    }
}




Answers (4)