How to insert JPEG into a SQL Server 2000 database field of image type using Transact SQL

∥☆過路亽.° 提交于 2019-11-28 07:30:55

问题


I'm trying to figure out how to insert a .JPG file into a SQL Server 2000 database field of type image using Transact SQL. Thanks.


回答1:


Use OPENROWSET:

INSERT MyTable (ImageColumnName) 
SELECT BulkColumn FROM OPENROWSET (BULK 'c:\myjpeg.jpg', SINGLE_BLOB) AS X

EDITED Whoops, you're using 2000--the previous solution is not supported. You have to use WRITETEXT:

CREATE TABLE MyTable 
(
    ID INT PRIMARY KEY IDENTITY (1,1), 
    ImageColumnName IMAGE NULL
)
GO

-- must insert a dummy value into the image column for TEXTPTR 
-- to work in next bit
DECLARE @RowId INT
INSERT MyTable (ImageColumnName) VALUES (0xFFFFFFFF)
SELECT @RowId = SCOPE_IDENTITY()

-- get a pointer value to the row+column you want to 
-- write the image to
DECLARE @Pointer_Value varbinary(16)
SELECT @Pointer_Value = TEXTPTR(ImageColumnName)
FROM MyTable
WHERE Id = @RowId

-- write the image to the row+column pointer
WRITETEXT MyTable.ImageColumnName @Pointer_Value 'c:\myjpeg.jpg'



回答2:


There is a tool called textcopy.exe You can find it under MSSQL\Binn or get it with SQL Server 2000 SP4

Alexander Chigrik wrote a nice stored procedure for usinig it with SQL query:

http://www.mssqlcity.com/Articles/KnowHow/Textcopy.htm




回答3:


The stored procedure found in this tutorial worked for me:

Brief tutorial on text, ntext, and image



来源:https://stackoverflow.com/questions/1214462/how-to-insert-jpeg-into-a-sql-server-2000-database-field-of-image-type-using-tra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!