SQL Server 2000:如何从存储过程将图像变量保存到文件系统中的文件

发布于 12-19 16:41 字数 102 浏览 4 评论 0原文

我有一个接收 @Data image 参数的存储过程。

我想将它从存储过程保存到文件系统。

知道怎么做吗?

I have a stored procedure that receives a @Data image parameter.

And I want to save it to the file system from a stored procedure.

Any idea how to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若水微香2024-12-26 16:41:56

您可以使用 OLE 自动化 (ADODB.Stream) 从 SQL Server 2000 存储过程写入二进制数据,如 这里这里。下面的示例使用 varbinary 变量,但它应该与您的图像参数类似地工作。

DECLARE @Path VarChar(255)
DECLARE @Data VarBinary(1000)
SET @Data = 0x12345678
SET @Path = 'c:\test.dat'

DECLARE @object Integer
DECLARE @hr Integer
-- create the ADO stream object
EXEC @hr = sp_oacreate 'ADODB.Stream', @object OUTPUT, 1
IF (@hr <> 0)
   RAISERROR('ADO stream creation failed', 11, 0)
-- configure it for binary access
EXEC @hr = sp_oasetproperty @object, 'Type', 1
IF (@hr <> 0)
   RAISERROR('Failed to set stream type', 11, 0)
-- open the stream
EXEC @hr = sp_oamethod @object, 'Open'
IF (@hr <> 0)
   RAISERROR('Failed to open the stream object', 11, 0)
-- write the buffer to the stream
EXEC @hr = sp_oamethod @object, 'Write', NULL, @Data
IF (@hr <> 0)
   RAISERROR('Failed to write a buffer to the stream', 11, 0)
-- save the stream to a file with overwrite
EXEC @hr = sp_oamethod @object, 'SaveToFile', NULL, @Path, 2
IF (@hr <> 0)
   RAISERROR('Failed to save the stream to a file', 11, 0)
-- cleanup
EXEC sp_oadestroy @object

请注意,通过此方法(以及我所知道的 SQL2000 中的所有其他方法)对文件系统的访问将发生在 SQL Server 进程的安全上下文中,而不是存储过程的调用者。

如果您不习惯将 ADO 组件加载到 SQL Server 进程中,您可以考虑实现 扩展存储过程来写入您的文件。

You can use OLE automation (ADODB.Stream) to write binary data from a SQL Server 2000 stored procedure, as described here and here. The sample below uses a varbinary variable, but it should work similarly with your image parameter.

DECLARE @Path VarChar(255)
DECLARE @Data VarBinary(1000)
SET @Data = 0x12345678
SET @Path = 'c:\test.dat'

DECLARE @object Integer
DECLARE @hr Integer
-- create the ADO stream object
EXEC @hr = sp_oacreate 'ADODB.Stream', @object OUTPUT, 1
IF (@hr <> 0)
   RAISERROR('ADO stream creation failed', 11, 0)
-- configure it for binary access
EXEC @hr = sp_oasetproperty @object, 'Type', 1
IF (@hr <> 0)
   RAISERROR('Failed to set stream type', 11, 0)
-- open the stream
EXEC @hr = sp_oamethod @object, 'Open'
IF (@hr <> 0)
   RAISERROR('Failed to open the stream object', 11, 0)
-- write the buffer to the stream
EXEC @hr = sp_oamethod @object, 'Write', NULL, @Data
IF (@hr <> 0)
   RAISERROR('Failed to write a buffer to the stream', 11, 0)
-- save the stream to a file with overwrite
EXEC @hr = sp_oamethod @object, 'SaveToFile', NULL, @Path, 2
IF (@hr <> 0)
   RAISERROR('Failed to save the stream to a file', 11, 0)
-- cleanup
EXEC sp_oadestroy @object

Note that access to the file system via this method (and all others in SQL2000 that I know) will occur under the security context of the SQL Server process, not the caller of the stored procedure.

If you aren't comfortable loading the ADO components into your SQL Server process, you could look into implementing an extended stored procedure to write your files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文