Click here to Skip to main content
15,795,793 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Insert Or Add Image in Database Using SQL SERVER
Sergey Alexandrovich Kryukov 23-Jan-12 3:22am    
Not a question!

First , you need to add column in your table with image datatype.
Then you have to convert image in byte array

string sPath ="";//your image name goes here
byte[] ydata = null;
FileInfo fInfo = new FileInfo(sPath);
long lNumBytes = fInfo.Length;

FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
//Use BinaryReader to read file stream into byte array.
BinaryReader br = new BinaryReader(fStream);
ydata = br.ReadBytes((int)lNumBytes);
return ydata;

You can use this generated byte array as a parameter value in Insert statement.
Share this answer
IN Database take 1 colomn Image with datatype image.

save code :

FileUpload img = (FileUpload)FileUpload1;
Image3.ImageUrl = img.FileName;
Byte[] imgByte = null;
imgStoreVar = Image3.ImageUrl;
if (img.HasFile && img.PostedFile != null)
//To create a PostedFile
HttpPostedFile File = FileUpload1.PostedFile;
//Create byte Array with file len
imgByte = new Byte[File.ContentLength];
//force the control to load data in array
File.InputStream.Read(imgByte, 0, File.ContentLength);

insert imgByte into database
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900