我正在尝试将多个图像插入Image
到sql 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