The text of this article is not in this database — only its details are. Read it on the old site: Inserting & Retrieving Images from SQL Server Database without using Stored Procedures
29 Comments
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment.

G DragonPosted Apr 24, 2021, 4:52 PM
What is datatype in sql server?
Zikriya MuneerPosted Jul 8, 2019, 12:26 AM
Any one can help me
Zikriya MuneerPosted Jul 8, 2019, 12:26 AM
This is not download how can i do
Vora HimansuPosted Jan 29, 2019, 11:38 AM
Declare variable photo error please solve
Indrajeet BhattacharyaPosted Dec 30, 2017, 12:50 PM
The zip file is not downloading. Please suggest.
Bunty JainPosted Jul 12, 2017, 12:19 AM
Thanks.very good article.
Pradeep KumarPosted Jun 23, 2017, 5:54 PM
I get error in below code: pictureStudent.Image = null; if (reader["image"] != System.DBNull.Value) { byte[] photo_array = (byte[])reader["image"]; MemoryStream ms2 = new MemoryStream(photo_array); pictureStudent.Image = Image.FromStream(ms2); Error: Parameter is not valid. at System.Drawing.FromStream(Stream,Stream, Boolean useEmbeddedCouorManagement, Boolean validateImageData)
john.grimanis GrimanisPosted Oct 21, 2016, 6:07 AM
Good example, thanks
kumar sPosted Sep 23, 2014, 1:23 PM
ms.Position = 0; this statement made my code to work....thank you buddy...keep it up
kumar sPosted Sep 23, 2014, 1:22 PM
ms.Position = 0;
Prashanth ChindamPosted Jan 29, 2014, 7:23 AM
Instead of saving image in binary format, save the image physically in a folder and save the image path in database with data type as nvarchar. use image url eg: http://abc.com/images/sampleimage.jpg, to display image in email content.
Filipe VilhenaPosted Jan 29, 2014, 6:54 AM
hello, I have a similar situation to what you describe here, although I need to get the image cid when getting it from database (or stream) in order to insert it in my email body, replacing the current LinkedResource which doesn't display anything. Can you help?
Santosh YadavPosted Jan 29, 2014, 1:09 AM
good article indeed, thanks..
ashok kumarPosted Jan 23, 2012, 6:32 AM
I have table like this.. SLNO UNITNO PHOTOBARCODE -------------------------------------- 1 C45678 <NULL> 2 C89678 <NULL> 3 C47878 <NULL> 4 D65678 <NULL> .... .... 1000 H45678 <NULL> I WANT TO GENERATE BARCODE FOR THE UNITNO AND WANT TO STORE THE BARCODE IMAGE IN DATABASE USING FORLOOP... PLEASE HELP ME... WHILE I USED U'R CODE...ITS SHOWS AN ERROR MESSAGE AS...."MUST DECLARE SCALAR VARIABLE @PHOTO"
sunil jadhavPosted Nov 18, 2011, 1:49 AM
sir i am doing one project in vb 6.0 so please can you give me that same code for vb 6.0 instead of vb.net
AmolPosted Oct 30, 2011, 3:38 AM
GR8 WORK!!!
Venkatesh MRPosted Apr 6, 2011, 3:03 AM
Nice example
Techni AllPosted Apr 5, 2011, 12:59 AM
Hi I ve uploaded my image binary from VC++6 to SQL server. On the web end, I am using C# commands to display these image but its not working. A possible problem may be: I am uploading the image in binary format but SQL DATAtype is IMAGE. C# is using showimage(). Your help will be highly appreciated.
Ketul RathodeditedPosted Feb 2, 2011, 10:58 PMEdited Feb 2, 2011, 11:17 PM
Does it work with Sql Express Edition or we have to install SQL server 2005 Externally..... reply immediately....
kishore kumarPosted Jan 18, 2011, 9:12 PM
hi,im kishore im saving image successfully,but while retreiving gettingerror as parameter is not valid, is there any mistake in my code? here is my code...pls help me... thank u in adv... using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging ; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Data.SqlClient; namespace WindowsFormsApplication1 { public partial class Form1 : Form { SqlConnection con = new SqlConnection("user id=sa;password=kiran;database=kiran"); MemoryStream ms; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { openFileDialog1.ShowDialog(); pictureBox1.ImageLocation = openFileDialog1.FileName; } private void button2_Click(object sender, EventArgs e) { SqlDataAdapter da = new SqlDataAdapter("insert into img values(" + textBox1.Text + ",'" + pictureBox1.ImageLocation + "')", con); DataSet ds = new DataSet(); da.Fill(ds, "img"); ms = new MemoryStream(); pictureBox1.Image.Save(ms, ImageFormat.Jpeg); byte[] data = new byte[ms.Length]; ms.Position = 0; ms.Read(data, 0, Convert.ToInt32(ms.Length)); MessageBox.Show("Saved"); } private void button3_Click(object sender, EventArgs e) { // SqlDataAdapter da = new SqlDataAdapter("select * from img where id=" + textBox1.Text, con); SqlCommand cmd = new SqlCommand("img_ret", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@id", textBox1.Text); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds, "img"); byte[] data = (byte[])ds.Tables["img"].Rows[0][0]; ms = new MemoryStream(data); pictureBox1.Image= Image.FromStream(ms); } } }
jagadeesh seelamPosted Jan 3, 2011, 12:18 PM
nice..,.
mahesh naniPosted Nov 1, 2010, 4:26 AM
thanks..........