Hi,
How can I save image into database and retrieve image at front-end form database in ado.net?
Thanks.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Jan 11, 2012, 1:18 AM
Please refer Our recommended articles.
Thanks
Datta KharadPosted Jan 11, 2012, 1:16 AM
I have done using oracle database.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;
using System.IO;
using System.Drawing.Imaging;
namespace Insert_Application
{
public partial class frm_insert : Form
{
OracleConnection con;
OracleCommand cmd;
MemoryStream ms;
OracleDataReader dr;
OracleDataAdapter da;
DataSet ds;
public frm_insert()
{
InitializeComponent();
}
private void btnclear_Click(object sender, EventArgs e)
{
clearfield ();
}
public void clearfield()
{
txtename .Text =txtsalary .Text = "";
}
private void btnclose_Click(object sender, EventArgs e)
{
this.Close();
}
private void frm_insert_Load(object sender, EventArgs e)
{
con = new OracleConnection("Data Source=ndsinsur; User ID=vbproj; Password=vbproj");
con.Open();
incrementid();
cmd.CommandText = "select eid from empdbimg";
cmd.Connection = con;
dr = cmd.ExecuteReader();
while (dr.Read())
{
comboBox1.Items.Add(dr[0]);
}
dr.Close();
}
public void incrementid()
{
cmd = new OracleCommand();
cmd.CommandText = "select MAX(eid+1) from empdbimg";
cmd.Connection = con;
dr = cmd.ExecuteReader();
while (dr.Read())
{
txteid.Text = dr[0].ToString();
}
dr.Close();
txtename.Focus();
}
private void btnsave_Click(object sender, EventArgs e)
{
cmd = new OracleCommand();
cmd.Parameters.Clear();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into empdbimg(eid,ename,salary,hiredate,photo)values(:eid,:ename,:salary,:hiredate,:photo)";
cmd.Parameters.Add (":eid",txteid .Text );
cmd.Parameters.Add (":ename",txtename .Text );
cmd.Parameters.Add(":salary", txtsalary.Text);
cmd.Parameters.Add (":hiredate",dtp1 .Text );
//Save image into database
ms = new MemoryStream();
pictureBox1 .Image .Save(ms ,ImageFormat .Jpeg );
byte[] photo = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo, 0, (int)ms.Length);
cmd.Parameters.Add(":photo",photo );
cmd.Connection = con;
cmd.ExecuteNonQuery();
MessageBox.Show("Record Has Saved Successfully...!!!");
clearfield ();
incrementid();
}
private void btnphotoload_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Open Image File";
ofd.Filter = "Bitmap Files|*.bmp" + "|Gif Files|*.gif|Icons|*.ico|JPEG Files|*.jpg" +
"|PNG Files|*.png|TIFF Files|*.tif";
ofd.ShowDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
txtimgpath.Text = ofd.FileName;
}
else
{
MessageBox.Show("Please select image");
}
pictureBox1.ImageLocation = txtimgpath.Text;
pictureBox1.Show();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
cmd.Parameters.Clear();
cmd.CommandText = "select * from empdbimg where eid=:eid";
cmd.Parameters.Add(":eid", comboBox1.SelectedItem.ToString());
da = new OracleDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds, "empdbimg");
cmd.Connection = con;
if(ds.Tables [0].Rows .Count >0)
{
txtename.Text = ds.Tables [0].Rows [0][1].ToString ();
txtsalary.Text = ds.Tables [0].Rows [0][2].ToString();
dtp1.Text = ds.Tables[0].Rows[0][3].ToString();
//Here Read Image from database
byte[] photo = (byte [])ds.Tables[0].Rows[0][4];
ms = new MemoryStream(photo);
pictureBox1.Image = Image.FromStream(ms);
}
dr.Close();
con.Close();
}
}
}
Mukesh KumarPosted Jan 11, 2012, 12:25 AM
Check this link
http://www.codeproject.com/KB/database/ImageSaveInDataBase.aspx