Click here to Skip to main content
16,019,983 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Can I directly store images in a database.

I don't want to insert it from code.

Is it possible ?
Posted
Updated 13-Jun-11 21:13pm
v2
Comments
Dalek Dave 14-Jun-11 3:15am    
Edited for Grammar and Readability.

you can do this way:

SQL
CREATE TABLE myTable(Document varbinary(max))
INSERT INTO myTable(Document)
SELECT * FROM
OPENROWSET(BULK N'C:\Image1.jpg', SINGLE_BLOB)


if you want to insert multiple images do something like this:

SQL
DECLARE @imgString varchar(80)
DECLARE @insertString varchar(3000)

SET @imgNumber = 1
WHILE @imgNumber < 101
BEGIN
SET @imgString = 'E:\images\Picture' + CONVERT(varchar,@imgNumber) + '.jpg'
SET @insertString = N'INSERT INTO images(imageData)
SELECT * FROM OPENROWSET(BULK N''' + @imgString + ''', SINGLE_BLOB) as tempImg'
EXEC(@insertString)
SET @imgNumber = @imgNumber + 1
END
GO


hope this helps :)
 
Share this answer
 
Comments
Dalek Dave 14-Jun-11 3:15am    
Good Answer.
Uday P.Singh 14-Jun-11 3:24am    
thanks Dave :)
thatraja 14-Jun-11 6:49am    
Hi, use Reply link on comments if you want reply to the comment so that it will notify him(Like I did now).
Uday P.Singh 14-Jun-11 12:26pm    
thanks for sharing this..
Hi,
storing an image in a database is done by reading it as a binary data, and then saving it into the BLOB structure. You can find lots of examples on the web regarding this topic:
E.g.
Store and save image[^]
Store or Save images in SQL Server[^]
Images and SQL Server[^]
Regards
 
Share this answer
 
Comments
Dalek Dave 14-Jun-11 3:15am    
Good Call.
Just an addition to others: C# Photo Album Viewer[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900