用SQL语句从电脑导入图片到数据库

--创建图片表
CREATE TABLE W_PIC
(
    ID INT,                --编号
    WPATH VARCHAR(80),    --完整路径
    PIC VARCHAR(80),    --图片名称,不带后缀
    img image            --图片内容
)

--图片表中插入数据
INSERT INTO W_PIC(ID,WPATH,PIC)
SELECT 1, 'C:\Users\w\Desktop\产品图片\2#加工图34-C专用.jpg','2#加工图34-C专用'
UNION ALL
SELECT 2, 'C:\Users\w\Desktop\产品图片\129.jpg','129'

--创建游标
DECLARE CUR_PIC CURSOR FOR SELECT ID,WPATH,PIC FROM W_PIC;
DECLARE @ID int, @PATH VARCHAR(80), @PIC VARCHAR(80), @STR VARCHAR(100);
OPEN CUR_PIC;
FETCH NEXT FROM CUR_PIC INTO @ID, @PATH, @PIC;
WHILE @@FETCH_STATUS=0
BEGIN
    SET @STR=STR(@ID);    
    --插入图片数据
    EXECUTE ('UPDATE W_PIC SET img=(SELECT * FROM OPENROWSET(BULK N'''+@PATH+''', SINGLE_BLOB) AS Photo) WHERE ID='+@STR);
    FETCH NEXT FROM CUR_PIC INTO @ID, @PATH, @PIC;
END
CLOSE CUR_PIC;
DEALLOCATE CUR_PIC;

 

posted @ 2019-09-02 10:35  同尘  阅读(1688)  评论(0编辑  收藏  举报