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.
1
2
3
4
5
6
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:
1
2
3
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:
1
2
3
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# - Performance Counters

ASP.NET MVC - Disabling Browser Link