最新做数据导入,需要将文件写入到Image字段,尝试了使用SQL的OpenRowSet实现,将过程整理如下:
以SQL2005为例:
1.打开OpenRowSet 和OpenDataSource支持:
(1)向导式:
开始 —>
所有程序 —>
Microsoft SQL Server 2005 —>
配置工具 —>
SQL Server外围应用配置器 —>
功能的外围应用配置器 —>
实例名 —>
Database Engine —>
即席远程查询 —>
启用OpenRowset和OpenDatasource支持。
(2)代码启用/关闭
启用:
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
关闭:
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure
2.应用举例:
使用OpenRowSet 将文件写入到Image字段:
Create Table #t (fFileName nvarchar(500),fFileContent Image,fFileLength int)
Insert into #t
select 'Img.png',BulkColumn,DataLength(BulkColumn)
From OpenRowSet(Bulk'G:\Windows\Web\Wallpaper\Scenes\img27.jpg', SINGLE_BLOB) AS [Document]
其中 OpenRowSet(Bulk'FileName' ,Single_Blob)始终会产生BulkColumn列,其内容就是文件的Binary。