2

我正在尝试将多个图像插入Imagesql server 2012.

该文件夹有多个.jpg.png文件。

我目前正在insert一一使用这个sql

如何插入多个图像或整个文件夹谢谢。

INSERT INTO Images(ImageId, ItemId, Caption, Image)
Values(NEWID(), null, null,(  
SELECT * FROM OPENROWSET(
   BULK 'C:\Images\image004.jpg',
   SINGLE_BLOB) AS x))

这是 ClusterKey 列上具有聚集索引的表

CREATE TABLE [dbo].[Images](
    [ImageId] [uniqueidentifier] NOT NULL,
    [ItemId] [uniqueidentifier] NULL,
    [Caption] [nvarchar](100) NULL,
    [Image] [image] NOT NULL,
    [ClusteredKey] [int] IDENTITY(1,1) NOT NULL,
 CONSTRAINT [PK_Images] PRIMARY KEY NONCLUSTERED 
(
    [ImageId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

目前正在尝试这种方法

--Table to process the files

CREATE TABLE imagelist 
  ( 
     imgfilename VARCHAR(200) 
  ) 
GO 
--Put all file name in a table for easy processing

DECLARE @SQL AS NVARCHAR(2000) 
SET @SQL =N'xp_cmdshell ''dir c:\Images /B''' 
INSERT INTO imagelist (imgfilename) EXEC sp_executesql @SQL
GO
--Import data into target table

DECLARE @SQL AS NVARCHAR(2000) 
DECLARE @ImgFilename AS VARCHAR(200) 
DECLARE filelist CURSOR FOR 
  SELECT imgfilename 
  FROM   imagelist 

OPEN filelist 

FETCH next FROM filelist INTO @ImgFilename 

WHILE ( @@FETCH_STATUS = 0 ) 
  BEGIN 
      SET @SQL = 'INSERT INTO Images(ImageId, Image)   Values(NEWID(),( select  reverse(substring(REVERSE(''' 
                 + @ImgFilename 
                 + ''') ,charindex(''.'',REVERSE(''' 
                 + @ImgFilename + ''' ),1)+1,LEN(''' 
                 + @ImgFilename 
                 + '''))) , (SELECT img.bulkcolumn FROM OPENROWSET(BULK ''c:\Images\' 
                 + @ImgFilename + ''',SINGLE_BLOB) AS img))' 

      EXEC Sp_executesql 
        @SQL 

      FETCH next FROM filelist INTO @ImgFilename 
  END 

CLOSE filelist 

DEALLOCATE filelist 
4

1 回答 1

1

这显示了一种导入可以计算文件名的多个文件的方法:

declare @Count as Int = 3622;
declare @Filename as VarChar(128);
declare @Statement as VarChar(256)

while @Count <= 3625
  begin
  set @Filename = 'C:\Photos\IMG_' + Right( '000' + Cast( @Count as VarChar(5) ), 4 ) + '.JPG';
  set @Statement = 'select * from OpenRowSet( Bulk ''' + @Filename + ''', Single_Blob ) as Nought';
  execute( @Statement )
  set @Count = @Count + 1;
  end;

不漂亮,但如果这是一次性导入,那么它可能无关紧要。

于 2013-05-29T23:44:09.490 回答