SQL - Inserting Images

If there is ever a need to store images into SQL Server, it can be done in a few simple steps; you will need to create the database, the table names and insert your images.
CREATE TABLE ImageResource (                         
             ID INT(5) NOT NULL AUTO_INCREMENT,
             NAME NVARCHAR(255) NULL,          
             IMAGE IMAGE NULL,                                         
             PRIMARY KEY  ('ID')                   
);
Once the table has been created, you simply do the insert via the following:
INSERT INTO dbo.ImageResource (NAME, IMAGE)                         
             SELECT 'LOGO_NAME' AS NAME, *
             FROM OPENROWSET (BULK N'C:\FILENAME.PNG', SINGLE_BLOB) AS IMAGE
If the image needs to be updated, use the following:
UPDATE dbo.ImageResource SET IMAGE = (
             SELECT * FROM OPENROWSET(BULK N'C:\FILENAME.PNG', SINGLE_BLOB) AS IMAGE 
             ) WHERE NAME = 'LOGO_NAME'

Comments

Popular posts from this blog

C# - ListView Item Spacing (Padding)

C# / SQL - Performing Distributed Transactions

IIS / ASP.NET - Disabling Compatibility Mode/View (Internet Explorer)