直接从SQL Server查询将图像保存到磁盘

时间:2011-07-15 19:23:37

标签: sql sql-server image sql-server-2008 save

我将在这个问题前面加上这样一个事实:我不知道这是否可能,而且我对sql或文件结构一无所知。

我有一个带有image类型列的SQL Server数据库。我想将其中一个图像保存到磁盘而不必编写程序(如果可能)。因此,在理想的世界中,我将在Sql Server Management Studio中运行查询。结果单元格上的RMB,并将图像另存为磁盘。

这当然不起作用,但我能追求的是什么?我搜索了SO和谷歌,但找不到任何东西。

2 个答案:

答案 0 :(得分:1)

您使用的是哪个版本的SQL Server? SQL 2008支持FILESTREAM,您可以在其中将BLOB数据直接保存到NTFS卷。这里有一篇很好的博客文章: http://blogs.msdn.com/b/rdoherty/archive/2007/10/12/getting-traction-with-sql-server-2008-filestream.aspx

答案 1 :(得分:0)

试试这个:

declare @ObjectToken int
declare @signature varbinary(8000)  --  Load @signature with the value of your image, I have not tested varbinary(max) with sp_oacreate
declare @fullname varchar(500)  --  Load @fullname with folder and filename to export to

exec sp_oacreate 'ADODB.Stream', @objecttoken output
exec sp_oasetproperty @objecttoken, 'type', 1
exec sp_oamethod @objecttoken, 'open'
exec sp_oamethod @objecttoken, 'write', null, @signature
exec sp_oamethod @objecttoken, 'savetofile', null, @FullName, 2
exec sp_oamethod @objecttoken, 'close'
exec sp_oadestroy @objecttoken