You are on page 1of 2

using System;

using System.Data.SqlClient;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace imageDBase
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

string imagePath = "";


private void buttonbrowse_Click(object sender, EventArgs e)
{
OpenFileDialog obj = new OpenFileDialog();
if (DialogResult.OK == obj.ShowDialog())
{
pictureBox1.BackgroundImage = Image.FromFile(obj.FileName);
pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;
imagePath = obj.FileName;
}
}

private void buttonSave_Click(object sender, EventArgs e)


{
if (textBoxtId.Text.Length == 0)
{
MessageBox.Show("Enter Student's Id");
}
else
{
try
{

SqlConnection cn = new SqlConnection(@"Data


Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Administrator\My
Documents\std_image1.mdf;Integrated Security=True;User Instance=True");
cn.Open();
string sql = "insert into Image values('" + textBoxtId.Text +
"',@pic)";
SqlCommand cmd = new SqlCommand(sql, cn);
FileStream fs = new FileStream(imagePath, FileMode.Open,
FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] b = br.ReadBytes(int.Parse(fs.Length.ToString()));
cmd.Parameters.Add("@pic", SqlDbType.Image).Value = b;
cmd.ExecuteNonQuery();
cn.Close();
MessageBox.Show("Image saved successfully", "Message",
MessageBoxButtons.OK, MessageBoxIcon.Information);
textBoxtId.Text = null;
}
catch (Exception ex)
{
MessageBox.Show("Duplication of Std_Id");
textBoxtId.Text = null;
}
}
}

private void buttonExit_Click(object sender, EventArgs e)


{
this.Close();
}

private void buttonDis_Click(object sender, EventArgs e)


{
try
{

SqlConnection cn = new SqlConnection(@"Data


Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Administrator\My
Documents\std_image1.mdf;Integrated Security=True;User Instance=True");
cn.Open();
string sql = "select pic from Image where s_id = '" +
textBoxSearch.Text + "'";
SqlDataAdapter da = new SqlDataAdapter(sql, cn);
DataSet ds = new DataSet();
da.Fill(ds);
byte[] b = (byte[])ds.Tables[0].Rows[0]["pic"];
MemoryStream ms = new MemoryStream(b);
cn.Close();
pictureBox2.BackgroundImage = Image.FromStream(ms);
pictureBox2.BackgroundImageLayout = ImageLayout.Zoom;
textBoxSearch.Text = null;
}
catch (Exception ex)
{
MessageBox.Show("Std_id Not Found");
textBoxSearch.Text = null;
}
}

private void button1_Click(object sender, EventArgs e)


{
}

private void textBoxtId_Enter(object sender, EventArgs e)


{
pictureBox2.BackgroundImage = null;
}

private void textBoxSearch_Enter(object sender, EventArgs e)


{
pictureBox1.BackgroundImage = null;
}
}
}

You might also like