Insert an Image:
OpenFileDialog dlg = new OpenFileDialog(); dlg.ShowDialog(); FileStream fs = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read); byte[] data = new byte[fs.Length]; fs.Read(data, 0, System.Convert.ToInt32(fs.Length)); fs.Close(); SqlConnection con = new SqlConnection("server=.\\sqlexpress;Initial catalog=db;Integrated Security=true"); con.Open(); SqlCommand cmd; cmd = new SqlCommand("insert into img values('" + data + "')", con); cmd.ExecuteNonQuery();
Retrieve an Image:
cmd = new SqlCommand("select * from img", con); DataTable dt = new DataTable(); SqlDataAdapter da; da = new SqlDataAdapter(cmd); da.Fill(dt); byte[] MyData = (byte[])dt.Rows[0]["Images"]; MemoryStream stream = new MemoryStream(MyData); pictureBox1.Image = Image.FromStream(stream);
CREATE TABLE [dbo].[Img]( [Id] [int] IDENTITY(1,1) NOT NULL, [Images] [image] NULL )
Error:
While inserting an image no error.
But while retrieving an image "Parameter is not valid." error came.
Can anyone Pls give the soln.
Thanks in Adavance.