Images and SQL ServerBy Bill Graziano on 6 November 2000 | Tags: Application Design One of our most asked questions is "How do you store images (BMP, JPG, GIF, etc.) in SQL Server?" In this article we'll discuss the options, the pitfalls and try to point you in the right direction. I don't have all the answers yet but you should be able to get started looking in the proper places.
Every time someone asks how to store images in SQL Server I have the same response: YOU DON'T. SQL Server is not the best tool to store images. In fact it's not even a good tool to store images. Especially if you want to display them in a browser. You can store images in SQL Server but I'm going to try and convince you not too. If you decide you still want to, I'll try to help you get started. In this article we'll cover pulling images out of SQL Server. In a future article I'll cover storing the images in SQL Server. I'll also give you enough links that you should be able to write your own routines to store the images if I don't get to it soon enough.
Linking to ImagesThe best way for SQL Server to deal with images is for SQL Server to store a pointer to an image. This can be a file name, directory/file combination or URL. The actual image can be stored on the file system or on the web server. It's very easy to write ASP code to build an IMG tag. The tag is customized with the file name pulled from the database. I'll walk through an example of this using ASP. For example, I have code to select a banner image and display it. My table looks something like this:CREATE TABLE [BANNER_Ads] ( [AdId] [int] IDENTITY (1, 1) NOT NULL , [AdName] [char] (100) NOT NULL , [AdType] [char] (10) NOT NULL , [ImageURL] [varchar] (100) NULL , [Height] [smallint] NULL , [Width] [smallint] NULL , [ALTText] [varchar] (100) NULL , [HTML] [varchar] (2000) NULL , [LinkToURL] [varchar] (100) NULL , ) ON [PRIMARY] GOThe SELECT statement is also pretty basic: SELECT * FROM BANNER_Ads WHERE AdId = 17And here is the ASP script to generate the IMG tag.
SELECTing ImagesNow we'll retrieve images from SQL Server using ASP. In case you've forgotten (or I didn't mention it enough above) this is really a pain. You are going to need two separate ASP Pages. This code will pulled from a Microsoft Knowledge Base article. The first I called Picture.asp and it looks like this: This calls the ASP page PicShowImage.asp which looks like this:
|
- Advertisement - |