hi
I am trying to save multiple images into one ID and I am getting no where ...I am posting the code so for storing and retrieving image (one image)...but need to store more than one ....need some help here ...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace TransportProject
{
public partial class ImageForm : Form
{
string imagename;
SqlDataAdapter empadap1;
DataSet dset;
public ImageForm()
{
InitializeComponent();
}
private void ImageForm_Load(object sender, EventArgs e)
{
Connection();
}
private void updatedata()
{
//use filestream object to read the image.
//read to the full length of image to a byte array.
//add this byte as an oracle parameter and insert it into database.
try
{
//proceed only when the image has a valid path
if (imagename != "")
{
FileStream fs;
fs = new FileStream(@imagename, FileMode.Open, FileAccess.Read);
//a byte array to read the image
byte[] picbyte = new byte[fs.Length];
fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
//open the database using odp.net and insert the data
string connstr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\admin\Desktop\TransportProject\TransportProject\bin\Debug\Database\TransportDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
string query;
query = "insert into test_table(id_image,pic) values('" +
textBox1.Text + "'," + " @pic)";
SqlParameter picparameter = new SqlParameter();
picparameter.SqlDbType = SqlDbType.Image;
picparameter.ParameterName = "pic";
picparameter.Value = picbyte;
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.Add(picparameter);
cmd.ExecuteNonQuery();
MessageBox.Show("Image Added");
cmd.Dispose();
conn.Close();
conn.Dispose();
Connection();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Connection()
{
//connect to the database and table
//selecting all the columns
//adding the name column alone to the combobox
try
{
string connstr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\admin\Desktop\TransportProject\TransportProject\bin\Debug\Database\TransportDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
empadap1 = new SqlDataAdapter();
empadap1.SelectCommand = new SqlCommand("SELECT * FROM test_table"
, conn);
dset = new DataSet("dset");
empadap1.Fill(dset);
DataTable dtable;
dtable = dset.Tables[0];
comboBox1.Items.Clear();
foreach (DataRow drow in dtable.Rows)
{
comboBox1.Items.Add(drow[0].ToString());
comboBox1.SelectedIndex = 0;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
FileDialog fldlg = new OpenFileDialog();
//specify your own initial directory
fldlg.InitialDirectory = @":D\";
//this will allow only those file extensions to be added
fldlg.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif";
if (fldlg.ShowDialog() == DialogResult.OK)
{
imagename = fldlg.FileName;
Bitmap newimg = new Bitmap(imagename);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = (Image)newimg;
}
fldlg = null;
}
catch (System.ArgumentException ae)
{
imagename = " ";
MessageBox.Show(ae.Message.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
updatedata();
}
private void button3_Click(object sender, EventArgs e)
{
try
{
DataTable dataTable = dset.Tables[0];
//if there is an already an image in picturebox, then delete it
if (pictureBox2.Image != null)
{
pictureBox2.Image.Dispose();
}
//using filestream object write the column as bytes and store it as an image
FileStream FS1 = new FileStream("image.jpg", FileMode.Create);
foreach (DataRow dataRow in dataTable.Rows)
{
if (dataRow[0].ToString() == comboBox1.SelectedItem.ToString())
{
byte[] blob = (byte[])dataRow[1];
FS1.Write(blob, 0, blob.Length);
FS1.Close();
FS1 = null;
pictureBox2.Image = Image.FromFile("image.jpg");
pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox2.Refresh();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
}