Friday, January 23, 2009

public partial class Form1 : Form
{
string path = "";
public Form1()
{
InitializeComponent();
}

private void button2_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
pictureBox1.ImageLocation= openFileDialog1.FileName;
path = openFileDialog1.FileName;
}

private void button1_Click(object sender, EventArgs e)
{
loaddata();
}

private void loaddata()
{

FileStream st = new FileStream(path, FileMode.Open);
byte[] buffer = new byte[st.Length];
st.Read(buffer, 0, (int)st.Length);
st.Close();

SqlConnection cn = new SqlConnection("User ID=sa;Initial Catalog=test;Data Source=.");
SqlCommand cmd = new SqlCommand("insert into emp values('"+textBox1.Text+"',@photo)",cn);
cmd.Parameters.Add("@photo", buffer);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}

private void button3_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("User ID=sa;Initial Catalog=test;Data Source=.");
SqlCommand cmd = new SqlCommand("select * from emp where empno=" + textBox2.Text,cn);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
textBox1.Text = dr[1].ToString();
byte[] image = (byte[])dr[2];
// System.Web.HttpContext.Current.Response.ContentType = "image/jpeg";

System.Drawing.Image _image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(image));

//System.Drawing.Image _newimage = _image.GetThumbnailImage(_width, _height, null, new System.IntPtr());

//_newimage.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

pictureBox1.Image = _image;
cn.Close();
}
}


CREATE TABLE [dbo].[emp] (
[empno] [int] IDENTITY (1, 1) NOT NULL ,
[ename] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[eimage] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

No comments: