【问题标题】:Convert image to nvarchar(max)将图像转换为 nvarchar(max)
【发布时间】:2020-12-26 06:44:27
【问题描述】:

我在我上传 pdf/word 文件/等的 MySQL 数据库中有一个字段定义为 longblob。 但是,我从我的 SQL Server 数据库访问此数据库,并且从链接服务器和视图访问数据。

问题 问题是,数据需要由另一个应用程序导入,但文档文件的数据类型是图像,这会导致问题。

我尝试了什么

我首先尝试查看是否将longblob 更改为varbinary 以获得SQL Server 中的varbinary(max) 数据类型。这不起作用,我仍然有图像。

然后我尝试制作视图以转换数据。我尝试了CAST(fileContent as varchar(max)) 并得到了错误:

不允许从数据类型 image 到 nvarchar(max) 的显式转换。

然后我按照我看到的建议尝试了cast(cast(fileContent as varbinary(max)) as varchar(max)) as fileContent,但我得到了大量的胡言乱语。

%PDF-1.4 %âãÏÓ 3 0 obj <</Type /Page /Parent 1 0 R /MediaBox [0 0 595.280 841.890] /TrimBox [0.000 0.000 595.280 841.890] /Resources 2 0 R /Group << /Type /Group /S /Transparency /CS /DeviceRGB >> /Contents 4 0 R>> endobj 4 0 obj <</Filter /FlateDecode /Length 521>> stream xœµRËnÛ0Ü3¿b-ÐÐ|ŠRn ) 4¥==¨¶âºu’F1

而不是像0x255044462D312E,

如果我能在转换二进制数据(声称它是图像到nvarchar(max))方面获得一些帮助,不胜感激。

【问题讨论】:

  • varchar(max) 和 nvarchar(max) 不适合二进制数据,因此您的胡言乱语,即:将二进制数据存储在二进制字段类型中,例如 varbinary(max)。
  • 哪种应用语言无法将 blob 转换为您喜欢的任何内容
  • @AlwaysLearning 我使用了你的答案,所以如果你发布为答案,我会接受它

标签: mysql sql-server


【解决方案1】:
declare @t table(theimage image)
insert into @t(theimage) 
values(0x3a26);


select convert(varchar(max), cast(theimage as varbinary(max)), 1) as theimagebinaryaschar
from @t;

declare @theimageaschar varchar(max) = '0x3a26';
select convert(varbinary(max), @theimageaschar, 1) as theimageasbinary;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    • 2018-06-08
    • 2022-01-19
    • 2019-08-25
    • 2012-10-18
    相关资源
    最近更新 更多