Introduction

This article is about storing and retrieving images from a SQL Server
database using VB.NET. When we create an application
where we need to save images then we save images in a folder and store the path of the image
in the database as string type.

If you save an image to a folder, you might accidentally delete the image from that folder. If this happens, you will get
an error when retrieving the image.
It is very difficult to handle these accidents.

So if you save an image into a database, you can enforce security by using the security settings of the database.

The application

Create a Windows application in VB.NET 2005 and design it as show in the above image. Then import namespaces as follows:

Imports System.Data.SqlClient
Imports System.IO

Create the database

Create a SQL Server database as follows. In Solution Explorer, click on project name and right click on it, then Add -> New item -> SQL,dDatabase
name "Database1.mdf", then OK. Click on database1 and create a table in it named information with fields as follows:

Field Name

Field Type

name

nvarchar(50)

photo

Image

Using the code

Actually the IMAGE field is just holding a reference to the page containing the binary data so we have to convert our image into bytes.