DIGIT-ARC read-only archive · snapshot: July 23, 2026

How to store Images in database table using VB.NET

11 posts · originally from https://geek.digit.in/community/threads/how-to-store-images-in-database-table-using-vb-net.140423/

bhutanesedude
bhutanesedude Original post #1
May 8, 2011 at 7:11 PM
Guys, I would like the experts to share us some insights about how to insert (upload) and retrive, along with deleting image, using a Windows Application.

Possibly, I would also like to know the limit of image to be uploaded such as through size (LxB) or through size in KB or MB etc.
arpanmukherjee1
May 8, 2011 at 10:09 PM
which DB ?

for SQL use 'image' data type
max size that can be uploaded using 'image' is 1.9GB

also use varbinary() with max size 7.8 MB if it fulfills the requirement
krishnandu.sarkar
May 9, 2011 at 1:21 AM
Or you can just store the path of the image in some field and retrieve it later while showing.
bhutanesedude
May 12, 2011 at 2:45 PM
Guys thank you for the response but what I really mean is, I am trying to develop a Stand-Alone Mini SOftware, in which a student details is saved in DB. Now I want it to be able to insert it's photo (Passport Size Photo) and retrive it whenever required. Well, I know that in DB Table the field type should be "image" but I want to know what should I write behind the coding of "upload" button to save that BROWSED image into the particular database. For instance I shall be using MDF database in VB.NET 2008.

Acctually I am a new bie in this and I tried googling it too but could not understand. Please help me.
A
May 12, 2011 at 9:27 PM
I think the second post by arpan is the bettr optn...using normal file classes of VB store the photos with the primary key...like for ur case store the photos in a folder say photos with d name of student id whch is ur primary key in the db...by this you dont need any new attribute in db...wen u r retreiving the details of a student u would obviously have to enter the student id...get the details of the student from the db n the photo yourself using the image class of vb giving the source "foldername/studentid.jpg" or what evr format you wnt to store....i have used the same thing and it works faster then using d db to store the images using image data type...
And still if you want to use db den to insert an image you simply do the same thing as you insert other values...just give the source path of the image you putting in the db...
bhutanesedude
May 27, 2011 at 7:07 PM
Thank you so much for the wonderful comments and indeed i tried the copying of file address instead of uplaoding the image, but now one problem I cam across, is like, if the user deletes the file in the original location, then theres the error...so now I would like to know if theres anyway to copy that particular Image(which ever is going to be uploaded) into one of the folder which will be in system file of the software......And if possible, pplease do let me know the ways as well the coding needed too....

Thank you to all once again.
krishnandu.sarkar
May 27, 2011 at 8:01 PM
Well, he said, copy the pic in to a directory say Images/ under your application folder. Now whenever you need to retrieve it, get it from the %APP%/Images/.

So if user deletes the original file, you have it's copy.
arpanmukherjee1
May 28, 2011 at 12:27 AM
here the %APP% = Application.StartupPath

if you have doubt in ADO.net refer to this
SQL syntax for inserting values

i recommend you read and understand the steps involved in running sql query :
1. connection string is defined. eg the DB is SQL 2008 then the string will be something like this
2. SQL command is generated
3. create SqlConnection object.
4. create SqlCommand object
5. open the connection
6. execute command object by any of 4 methods (depends on sql query i.e UPDATE, INSERT, or DELETE = ExecuteNonQuery otherwise ExecuteReader/ExecuteScalar)
7. dispose command object
8. dispose connection object

any more questions??
A
May 30, 2011 at 10:31 PM
In the database you don't have to give the location of the original folder from where the user is uploading the image...
1. Ask the user for the image path.
2. Copy the image from the path given by user, to a folder in your application say /images. This can be done using File.copy() method available in system.io(you might have to cross check) or use File.move() if you want to move image from users path to your apps path.
3. In the database enter the location of the image as per your application paths image and not of the original path given by the user.

P.S. for copying or moving files in the folder say program files/ ur app/ images, you would have to give your application administrative rights. For that add a manifest for your application and change a value to administrator....this is very simple just google it out( I cant remem properly about)


Hope this helps..cheers!
bhutanesedude
Jun 8, 2011 at 3:40 AM
Thank you to all...I understood....
Zangetsu
Zangetsu #11
Jun 8, 2011 at 10:21 AM
GNow I want it to be able to insert it's photo (Passport Size Photo) and retrive it whenever required. Well, I know that in DB Table the field type should be "image" but I want to know what should I write behind the coding of "upload" button to save that BROWSED image into the particular database.

its very simple...

declare a variable array of type byte.

e.g:
byte[] imageSize = new byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile uploadedImage = FileUpload1.PostedFile;
uploadedImage.InputStream.Read(imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);

then use dynamic sql query "insert into..." or stored procedure....pass the
parameter uploadedImage to the Command Text

thats it.