SQL Server 2008 Varbinary (Max)
SQL Server 2008 Varbinary (Max)
It is used to store Binary large Objects (BLOB) like images and other data files.Data will be stored as bits
in the database. This is preferred for files having size less then 1 MB. For larger files use varbinary(max)
filestream.
Steps to use varbinary to store and retrieve BLOB – Images in SQL Server 2008.
I. Inserting data.
CREATE TABLE dbo.ImageWarehouse
(
ImageWarehouseID INT IDENTITY (1,1) PRIMARY KEY,
ImageName VARCHAR(100),
Photo VARBINARY(MAX)
)
-- SQL Server import image - sql storing images database sql server
INSERT INTO dbo.ImageWarehouse (ImageName, Photo)
SELECT
'IMAGE.JPG',
*
FROM OPENROWSET
(
BULK ‘C:\file.jpg',SINGLE_BLOB
) AS x
II. Reading data.
Import the binary image to the HDD using the format file
-- SQL Server export image
-- Keep the command on ONE LINE - SINGLE LINE!!! - broken here for
presentation
SET @SQLcommand = 'bcp "SELECT Photo FROM XLB4000_MASTER.dbo.ImageWarehouse"
queryout "C:\myphoto.jpg" -T -f "C:\myFinal.fmt"'
EXEC xp_cmdshell @SQLcommand
NOTE:
To execute the xp_cmdshell command enable the advanced option as explained below.
RECONFIGURE
RECONFIGURE