如何将所有图像从 SQL Server 2008 中的 VARBINARY(MAX) 字段转储到文件系统? [英] How to dump all of our images from a VARBINARY(MAX) field in SQL Server 2008 to the filesystem?

查看:40
本文介绍了如何将所有图像从 SQL Server 2008 中的 VARBINARY(MAX) 字段转储到文件系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,其中有一个 IDENTITY 字段和一个 VARBINARY(MAX) 字段.有什么方法可以将每个 VARBINARY(MAX) 字段的内容转储到 filesystem (例如 c:\temp\images)?

I have a table which has an IDENTITY field and a VARBINARY(MAX) field in it. Is there any way I could dump the content of each VARBINARY(MAX) field to the filesystem (eg. c:\temp\images)?

PhotoId INTEGER NOT NULL IDENTITY
Image VARBINARY(MAX) NOT NULL
DateCreated SMALLDATETIME

输出:

c:\temp\images\1.png
c:\temp\images\2.png
c:\temp\images\3.png
... etc...

解决这个问题的最佳方法是什么?

What is the best way to approach this?

推荐答案

感谢 这篇文章 ...

SET NOCOUNT ON

DECLARE @IdThumbnail INTEGER,
        @MimeType VARCHAR(100),
        @FileName VARCHAR(200),
        @Sqlstmt varchar(4000)


DECLARE Cursor_Image CURSOR FOR
    SELECT a.IdThumbnail
    FROM tblThumbnail a
    ORDER BY a.IdThumbnail

OPEN Cursor_Image
    FETCH NEXT FROM Cursor_Image INTO @IdThumbnail

    WHILE @@FETCH_STATUS = 0
    BEGIN

        -- Generate the file name based upon the ID and the MIMETYPE.
        SELECT @FileName = LTRIM(STR(@IdThumbnail)) + '.png'

        -- Framing DynamicSQL for XP_CMDshell            
        SET @Sqlstmt='BCP "SELECT OriginalImage 
                      FROM Appian.dbo.tblThumbnail 
                      WHERE IdThumbnail = ' + LTRIM(STR(@IdThumbnail)) +
                      '" QUERYOUT c:\Temp\Images\' + LTRIM(@FileName) + 
                      ' -T -fC:\Temp\ImageFormatFile.txt'
        print @FileName
        print @sqlstmt

        EXEC xp_cmdshell @sqlstmt
        FETCH NEXT FROM Cursor_Image INTO @IdThumbnail
    END

CLOSE Cursor_Image
DEALLOCATE Cursor_Image

请注意 -> 你需要一个格式文件,用于 BCP 命令.这是文件的内容,我已将其放在 c:\Temp(如上面的 BCP 命令行中所述).

Please note -> u need to have a format file, for the BCP command. This is the content of the file and i've placed it in c:\Temp (as noted in the BCP commandline above).

10.0
1
1       SQLIMAGE            0       0       ""   1     OriginalImage                      ""

关于该格式文件的最后说明.. 在最后一行之后必须有一个新行.否则你会得到一个错误.

Final note about that format file .. there HAS TO BE A NEW LINE AFTER THE LAST LINE. otherwise u'll get an error.

享受吧!

这篇关于如何将所有图像从 SQL Server 2008 中的 VARBINARY(MAX) 字段转储到文件系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆