Retrieving Images from a Database

Introduction

In the first part of our article, we uploaded a file and stored it in our database. Now, we are going to stream binary data out of a database and to a browser client.

Refresher - Table Structure

Lets quickly refresh ourselves with the table structure we are using for storing images.

CREATE TABLE [dbo].[image] (
  [img_pk] [int] IDENTITY (1, 1) NOT NULL ,
  [img_name] [varchar] (50) NULL ,
  [img_data] [image] NULL ,
  [img_contenttype] [varchar] (50) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE [dbo].[image] WITH NOCHECK ADD
CONSTRAINT [PK_image] PRIMARY KEY NONCLUSTERED
(
  [img_pk]
) ON [PRIMARY]
GO

We have 4 columns in our sql server:

img_pk - our Identity primary key.
img_name - the friendly name we are using for our image
img_data - the binary data field we are storing our image in.
img_contenttype - the Mime type of our image, for example: image/gif

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Anyone who considers arithmetic methods of producing random digits is, of course, in a state of sin.” - John von Neumann