Thursday 13 December 2012

insert and retrive image from database in c#

Insert Image In Databse

public bool SaveImage()
        {
            FileStream fs = new FileStream(curFileName, FileMode.OpenOrCreate, FileAccess.Read);
            byte[] rawData = new byte[fs.Length];
            fs.Read(rawData, 0, System.Convert.ToInt32(fs.Length));
            fs.Close();
            try
            {
                if (curFileName != null)
                {
                    SqlCommand cmd = new SqlCommand("insert into ImageTable ([srno],[image]) values(@srno,@image)", con);
                    cmd.Parameters.Add("@srno", SqlDbType.NVarChar, 50).Value = textBoxSrNo.Text;
                    SqlParameter prm = new SqlParameter("@image", SqlDbType.Image, rawData.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, rawData);
                    cmd.Parameters.Add(prm);
                    con.Open();
                    cmd.ExecuteNonQuery();
                   
                    MessageBox.Show("Success");
                }
                else
                {
                    MessageBox.Show("Empty File Path");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
            finally
            {
                con.Close();
            }
            return true;
        }


retrive image from database

public void DisplayImage()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT image FROM ImageTable WHERE srno='" + textBoxSrNo.Text + "'", con);
                SqlDataAdapter sqlDA = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                sqlDA.Fill(ds, "ImageTable");
                int c = ds.Tables["ImageTable"].Rows.Count;
                if (c > 0)
                {
                    Byte[] byteBLOBData = new Byte[0];
                    byteBLOBData = (Byte[])(ds.Tables["ImageTable"].Rows[c - 1]["image"]);
                    MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
                    Image img;
                    //pictureBox1.Image = img.GetThumbnailImage(340, 165, null, new IntPtr());
                   
                   img = pictureBox1.Image = Image.FromStream(stmBLOBData);
                   pictureBox1.Image = img.GetThumbnailImage(200, 200, null, new IntPtr());
                    //pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                    pictureBox1.Show();

                    //Byte[] byteBLOBData = new Byte[0];
                    //byteBLOBData = (Byte[])(ds.Tables["ImageTable"].Rows[c - 1]["image"]);
                    //MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
                    //pictureBox1.Image = Image.FromStream(stmBLOBData);
                    //pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                    //pictureBox1.Show();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            con.Close();

        }





No comments:

Post a Comment