Home > Database > Mysql Tutorial > How to Export Image Field Data from Microsoft SQL Server to Individual Files?

How to Export Image Field Data from Microsoft SQL Server to Individual Files?

Mary-Kate Olsen
Release: 2024-12-27 03:53:37
Original
135 people have browsed it

How to Export Image Field Data from Microsoft SQL Server to Individual Files?

Exporting Image Field Data to a File

In Microsoft SQL Server, some tables may contain image fields that store file data. This file data, such as images, may need to be extracted and saved to individual files for various reasons. However, exporting this data directly from a database can be challenging.

Unfortunately, there is no built-in SQL script or function that allows direct export of image data to a file. To achieve this, you'll need to use a combination of T-SQL statements and OLE Automation Procedures.

Here's a detailed script that will enable you to export image data from a table into separate files:

-- Write all database images (jpg) to file.
--------- --------- --------- --------- --------- --------- --------- 
DECLARE CURSOR_ProductIds CURSOR FOR (SELECT ImgImagesId FROM ImgProductSample)

DECLARE @ProductId INT;

OPEN CURSOR_ProductIds

FETCH NEXT FROM CURSOR_ProductIds INTO @ProductId
WHILE (@@FETCH_STATUS <> -1)
BEGIN
  DECLARE @ImageData varbinary(max);
  SELECT @ImageData = (SELECT convert(varbinary(max), ImageData, 1) FROM ProductImages WHERE Id = @ProductId);

  DECLARE @Path nvarchar(1024);
  SELECT @Path = 'C:\MyImages\Output';

  DECLARE @Filename NVARCHAR(1024);
  SELECT @Filename = (SELECT ImageFilename FROM ProductImages WHERE id = @ProductId);

  DECLARE @FullPathToOutputFile NVARCHAR(2048);
  SELECT @FullPathToOutputFile = @Path + '\' + @Filename;

  DECLARE @ObjectToken INT
  EXEC sp_OACreate 'ADODB.Stream', @ObjectToken OUTPUT;
  EXEC sp_OASetProperty @ObjectToken, 'Type', 1;
  EXEC sp_OAMethod @ObjectToken, 'Open';
  EXEC sp_OAMethod @ObjectToken, 'Write', NULL, @ImageData;
  EXEC sp_OAMethod @ObjectToken, 'SaveToFile', NULL, @FullPathToOutputFile, 2;
  EXEC sp_OAMethod @ObjectToken, 'Close';
  EXEC sp_OADestroy @ObjectToken;

  FETCH NEXT FROM CURSOR_ProductIds INTO @ProductId
END
CLOSE CURSOR_ProductIds
DEALLOCATE CURSOR_ProductIds

-- Make sure the following statement is executed to enable file IO
-- From http://msdn.microsoft.com/en-us/library/ms191188.aspx
--------- --------- --------- --------- --------- --------- --------- 
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
Copy after login

This script uses ADO (ActiveX Data Objects) to create a stream object and manipulate the image data in memory. It then saves the data to a file using the Open, Write, and SaveToFile methods of the stream object.

The above is the detailed content of How to Export Image Field Data from Microsoft SQL Server to Individual Files?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template