SitePoint Sponsor |
|
User Tag List
Results 1 to 11 of 11
-
Nov 2, 2001, 12:07 #1
- Join Date
- Jun 2001
- Location
- Russia
- Posts
- 107
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
How to save an image in SQL Server
Could you, PLEASE, give me an example
of how to save an image in SQL Server?
Suppose I have a path to the image 'c:\image.gif'
and datafield in SQL Server database is 'Image'
What SQL statement should I write then ...?
Thank you
-
Nov 2, 2001, 18:33 #2
- Join Date
- Jan 2001
- Location
- Milton Keynes, UK
- Posts
- 1,011
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Try this MS KB article.
-
Nov 2, 2001, 21:31 #3
- Join Date
- Jul 2000
- Location
- Here
- Posts
- 1,010
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I haven't done this myself, but I believe you could read the data of the image and then just set it in a binary field. When you wanted to retrieve it, you could SELECT it from the database and, set the Content-Type to image and then dump the data. Like I said, I haven't done this, but in theory, it *should* work.
-
Nov 3, 2001, 07:23 #4
- Join Date
- Jun 2001
- Location
- Russia
- Posts
- 107
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The code sample in "MS KB article" requies
that I had ActiveX Data Objects (ADO) 2.5
and SQL Server 7
but
I have ActiveX Data Objects (ADO) 2.1 and SQL Server 6.5
I have tried the following code for the pubs database which is included in SQL Server
and has "Image" datafield. But it does not show me the pictures.
It writes all the characters in this data field. Where am I wrong?
Code:<%@ Language=VBScript %> <!-- #INCLUDE FILE="adovbs.inc" //--> <% Response.Expires = -1000 Response.Buffer = True 'Buffers the content so our Response.Redirect will work %> <HTML> <HEAD> <TITLE> New Document </TITLE> </HEAD> <BODY> <% Set cn = Server.CreateObject("ADODB.Connection") cn.Open "FILEDSN=pubs.dsn" Set rs = Server.CreateObject("ADODB.Recordset") rs.Open "Select * from pub_info", cn, adOpenKeyset, adLockOptimistic do while not rs.eof pic = rs.Fields("logo").value Response.ContentType = "image/GIF" response.BinaryWrite pic response.write "<br>---------------------------------------------------------------<br>" rs.MoveNext loop %> </BODY> </HTML>
Thank you.
-
Nov 3, 2001, 13:13 #5
- Join Date
- Jul 2000
- Location
- Here
- Posts
- 1,010
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Well, for one thing, you can't output HTML after you set the content type to image. If you want to retrieve the image, you need a specific page such as "viewimage.asp", which you call on as if it is an image in your page.
For instance, the code below would be your webpage, and an adaptation of the code you wrote would be viewimage.asp
Code:<img src="viewimage.asp" width="50" height="50">
-
Nov 3, 2001, 16:09 #6
- Join Date
- Jun 2001
- Location
- Russia
- Posts
- 107
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thank you Wes DeMoney.
Now, I know how to display images from SQL Server,
but now I want to write this image data onto SQL Server
I'm trying this code
Code:Set cn = Server.CreateObject("ADODB.Connection") cn.Open "FILEDSN=pubs.dsn" Set fs = CreateObject("Scripting.FileSystemObject") Set f = fs.GetFile("c:\5lakes.gif") Set ts = f.OpenAsTextStream(1, -2) str = ts.readline cn.execute("update pub_info set logo='" & str & "' where pub_Id='0736'")
Where am I wrong?
-
Nov 4, 2001, 09:11 #7
- Join Date
- May 2001
- Location
- :noitacoL
- Posts
- 1,859
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You may find this article on SQLTeam useful as well...
http://www.sqlteam.com./item.asp?ItemID=986
-
Nov 4, 2001, 10:19 #8
- Join Date
- Jun 2001
- Location
- Russia
- Posts
- 107
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hello hillsy.
The link you gave me - shows only how to display images
from a database.
But I need to know how to save this image
in database.
-
Nov 4, 2001, 11:08 #9
- Join Date
- May 2001
- Location
- :noitacoL
- Posts
- 1,859
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
True...
But I think they're basically saying you shouldn't store images in a SQL Server database, period. Putting them in and getting them out are really just two sides of the same coin.
-
Nov 4, 2001, 13:41 #10
- Join Date
- Jun 2001
- Location
- Russia
- Posts
- 107
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Could someone, PLEASE, show me the code how to save an image onto database !!!!
I'm using this code now, but it doesn't work.
Code:Set cn = Server.CreateObject("ADODB.Connection") cn.Open "FILEDSN=pubs.dsn" set RS = Server.CreateObject("ADODB.Recordset") rs.Open "pub_info1", Cn, 1, 2 Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.GetFile("c:\5lakes.gif") Set ts = f.OpenAsTextStream(1, -2) bytBLOB = ts.ReadAll rs.addnew rs.fields("pub_Id")="2" rs.fields("logo").AppendChunk bytBLOB rs.Update
but my boss wants me to save them in SQL Server,
because our SQL Server is backed up every 6 hours.
Thank you.
-
Nov 6, 2001, 08:15 #11
- Join Date
- Jul 2001
- Location
- Boston Area (North shore)
- Posts
- 233
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
If your server is backed up every 6 hours then what is the big deal of getting the images into the database? Unless the only thing on the whole server being backed up is SQL.
If this is the case then I would talk you your IT guys and have them backup the whole server then you can capture your images wherever they are stored on the server.
Just a suggestion from a SYS ADMIN. Seems to be a real easy solution for you rather than coding something that is no really suggested anyway for SQL like Hillsy said.
I know all my SQL guys along with myself never keep our images in the datbase. But everyone does things a little different across the board.Kevin Tremblay-- Sys Admin
work: kevin_tremblay@hsgmed.com
"The object of the journey is not to arrive."
Bookmarks