我想在 SQL Server 中存储大图像,然后在 Reporting Services、Power BI 和 Analysis Services 中使用它们。
我找到了一些帮助,但我仍然不明白存储它们的最佳方式是什么,以及如何以正确的格式转换这些图像。
我应该将它们转换为 Base64 吗?我怎么做?
我找到了关于Convert Image DataType To String in SQL Server、Storing images in SQL Server?,但他们都没有与我合作。
Convert Image DataType To String in SQL Server、Storing images in SQL Server?
所以我在我的数据库中,图像的路径,图像本身和扩展名如下:
SELECT NM_DIRETORIO AS NM_PATH , NM_FOTO AS NM_PICTURE, TP_EXTENSAO AS TP_EXTENSION FROM D_GB_FOTOS
示例数据库
正如我在这个视频中看到的SSRS - 从 SQL Server 数据库中读取图像,他varbinary(max)用来存储图像,但我不知道他是如何转换成那个的。此外,来自Chriss Webb 的:在 Power BI 数据集中存储大图像,他使用 Base64 在 Power BI 上显示。
所以我的问题是,由于我将使用大图像,如何将简单图像(路径 + 图片)转换为存储在我的 SQL Server 数据库中?
信息:
SQL Server 2019 (v15.0.18330.0)
SQL Server 管理对象 (SMO) v16.100.37971.0微软 SQL Server 管理工作室 v18.5 编辑:
根据@Peter Schneider 的回答,我创建了一个用于更新表的游标,具有该值。但是我在where子句上出错(例如:TABLE.ID_COLUMN)。
我的光标:
DECLARE @ID_FOTO INT; DECLARE @CD_ARQUIVO VARCHAR(4000); DECLARE @CD_ARQUIVO_VARBINARY VARCHAR(4000); DECLARE @tsql NVARCHAR (4000); DECLARE CUR CURSOR FOR SELECT ID_FOTO, CD_ARQUIVO, NM_DIRETORIO + '\' + NM_FOTO + TP_EXTENSAO AS CD_ARQUIVO_VARBINARY FROM D_GB_FOTOS WHERE LINORIGEM <> 'CARGA MANUAL' OPEN CUR FETCH NEXT FROM CUR INTO @ID_FOTO, @CD_ARQUIVO, @CD_ARQUIVO_VARBINARY WHILE @CD_ARQUIVO IS NULL BEGIN SET @tsql = 'UPDATE D_GB_FOTOS' + 'SET CD_ARQUIVO = (SELECT CD_ARQUIVO.* from Openrowset(Bulk' + @CD_ARQUIVO + ', Single_Blob) CD_ARQUIVO)' + 'WHERE ' + @ID_FOTO + ' = D_GB_FOTOS.ID_FOTO;' PRINT (@tsql) EXEC (@tsql) FETCH NEXT FROM cur INTO @ID_FOTO, @CD_ARQUIVO END CLOSE cur DEALLOCATE cur
编辑2:
对查询进行了一些调整,但还有一个最后的问题是它不断更新,并且不会以表的最终 ID 停止:
DECLARE @ID_FOTO INT; DECLARE @CD_ARQUIVO VARCHAR(4000); DECLARE @CD_ARQUIVO_VARBINARY VARCHAR(4000); DECLARE @tsql NVARCHAR (4000); DECLARE @ID_FOTO_MAX INT; SET @ID_FOTO_MAX = (SELECT MAX(ID_FOTO) AS ID_FOTO FROM D_GB_FOTOS); DECLARE CUR CURSOR FOR SELECT ID_FOTO, CD_ARQUIVO, (NM_DIRETORIO + '\' + NM_FOTO + TP_EXTENSAO) AS CD_ARQUIVO_VARBINARY FROM D_GB_FOTOS WHERE LINORIGEM <> 'CARGA MANUAL'; OPEN CUR FETCH NEXT FROM CUR INTO @ID_FOTO, @CD_ARQUIVO, @CD_ARQUIVO_VARBINARY WHILE (@ID_FOTO <= @ID_FOTO_MAX) BEGIN SET @tsql = 'UPDATE D_GB_FOTOS ' + 'SET CD_ARQUIVO = (SELECT CD_ARQUIVO from Openrowset(Bulk ''' + @CD_ARQUIVO_VARBINARY + ''', Single_Blob) CD_ARQUIVO)' + ' WHERE D_GB_FOTOS.ID_FOTO = ' + CONVERT(VARCHAR(10),@ID_FOTO) + ';' PRINT ('ID_FOTO: ' + CONVERT(VARCHAR(10),@ID_FOTO)) PRINT ('ID_FOTO_MAX: ' + CONVERT(VARCHAR(10),@ID_FOTO_MAX)) PRINT ('SELECT STATEMENT: ' + @tsql) EXEC (@tsql) FETCH NEXT FROM cur INTO @ID_FOTO, @CD_ARQUIVO, @CD_ARQUIVO_VARBINARY END CLOSE cur DEALLOCATE cur
我打印上面代码的示例:
(1 linha afetada) ID_FOTO: 6529 ID_FOTO_MAX: 6531 (1 linha afetada) ID_FOTO: 6530 ID_FOTO_MAX: 6531 (1 linha afetada)ID_FOTO:6531 ID_FOTO_MAX:6531 (1 linha afetada)ID_FOTO:6531 ID_FOTO_MAX:6531 (1 linha afetada)ID_FOTO:6531 ID_FOTO_MAX:6531
您可以使用 OpenRowSet 从磁盘读取图像并将其插入到您的表中
INSERT INTO YourTableName (ID, VarbinaryMaxColumn) VALUES (1, (SELECT * FROM OPENROWSET(BULK N'C:\Temp\Testimage.png', SINGLE_BLOB) AS VarbinaryMaxColumn) )