Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 99,191 times

Contents

Related Categories

Uploading Images to a Database - Introduction

dave123aspx

Introduction

Uploading images to a Sql Server database is extremely easy using ASP.NET and C#. This article will show you how to upload Images (or any Binary Data) to a SQL Server database using ASP.NET and C#. The next part in this series, Retrieving Images from a Database, will show you how extract images from a database.

Building the Database Table

We start out by building our database table. Our image table is going to have a few columns describing the image data, plus the image itself. Here is the sql required to build our table in SQL Server or MSDE.

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

I'm a great fan of having a single column primary key, and making that key an Identity column, in our example, that column is img_pk. The next column is img_name, which is used to store a friendly name of our image, for example "Mom and Apple Pie". img_data is actually our image data column, and is where we will be storing our binary image data. img_contenttype will be used to record the content-type of the image, for example "image/gif" or "image/jpeg" so we will know what content-type we need to output back to the client, in our case the browser.

Comments

  • Re: [3905] Uploading Images to a Database

    Posted by KingNetSurfer on 15 Jun 2006

    This may sound weird but I think I'm the only one not having perfect success with this.

    I'm getting a System.NullReferenceExc...

  • Image upload and thumbnails

    Posted by zioturo on 02 Nov 2005

    The easiest way to upload and resize an image to the internet and automatically create thumbnails is I-Load.
    I-Load is a FREE ASP.NET web control with numerous benefits and features.
    You can downloa...

  • Uploading

    Posted by harshguglani on 03 Sep 2005

    Hi, Whts the benifit of uploading images in database server when i can place images on web server in my directory and accesss them. Reply soon.

  • enctype?

    Posted by LesBillBell on 02 Aug 2005

    The explanatory text says "The first interesting point about our webform, is the attribute "enctype". Enctype tells the browser and server that we will be uploading some type of binary data" but I don...

  • Posted by LinuxGold on 08 Jul 2005

    Better control of file updates. On file server, anybody can update files without letting anyone know about it. If you store a file inside SQL database, it will show who last updated files, inform us...