SQLUSA

Microsoft SQL Server 2005
Administration Best Practices

How to import an image into the database?

 

Execute the following script to export image into the file system from the database and import the image back into a table:

USE AdventureWorks;

-- export
DECLARE @SQLcommand nvarchar(4000)
SET @SQLcommand = 'bcp "select LargePhoto from AdventureWorks.Production.ProductPhoto where ProductPhotoID = 111" queryout "f:\data\images\roadsterx.jpg" -T -n '

EXEC xp_cmdshell @SQLcommand

GO

-- import image
INSERT Production.ProductPhoto (
ThumbnailPhoto,
ThumbnailPhotoFileName,
LargePhoto,
LargePhotoFileName)
SELECT null, null, LargePhoto.*, N'roadsterx.jpg'
FROM OPENROWSET
(BULK 'f:\data\images\roadsterx.jpg', SINGLE_BLOB) LargePhoto
GO

select * from Production.ProductPhoto
GO

 

The Best SQL Server Training in the World
 
 
SQLUSA.com Home Page