Reading And Writing SQL Server - Image Datatype
Hey Guys,
I've been messing around with this script for a few nights.
I have created a field called ImageB in my tblUsers which is set to the "Image" datafield.
Now I'm downloading an image from the net and want to save it to that field instead of the file.
Here is the code i'm using:
WRITING
Code:
Function DownloadImage(strURL, lngID,strSavePath,strWebPath)
On Error Resume Next
' Create Work Object
Set objXMLHTTP = Server.CreateObject("Msxml2.ServerXMLHTTP")
Set objBinaryStream = CreateObject("ADODB.Stream")
' Get the file
objXMLHTTP.Open "GET", strURL , false
objXMLHTTP.Send()
objBinaryStream.Type = 1
objBinaryStream.Open
objBinaryStream.Write objXMLHTTP.responseBody
' Save the file on the server
Dim strFileName
strFileName = lngID & Day(Now()) & Month(Now()) & Year(Now()) & Minute(Now()) & Second(Now()) & ".jpg"
objBinaryStream.SaveToFile strSavePath & strFileName, 2
Set objXMLHTTP = Nothing
Set objBinaryStream = Nothing
If Err.number <> 0 Then
DownloadImage = ""
Else
DownloadImage = strWebPath & strFileName
End If
End Function
Now you will notice that with this example I'm actually saving the image to the file system. What I want to do is save it to the database instead of the file system.
I tried to do an insert statement like this:
Code:
INSERT INTO tblUsers SET ImageB='" & objBinaryStream.Read & "' WHERE ID=" & lngID
That doesn't seem to work, I don't get an error but it just doesn't seem to save anything.
Reading
Code:
Response.Expires = -10
Response.Buffer = True
Response.Clear
Response.ContentType = "image/JPEG" ' these ones are jpeg
Set cn = Server.CreateObject("ADODB.Connection")
cn.Open gstrMasterConnectionString
Set rs = cn.Execute("SELECT ImageB FROM tblUsers WHERE ID=" & Request("ID"))
Response.BinaryWrite rs("ImageB")
rs.Close
Set rs = Nothing
Set cn = Nothing
The code above shows the url that is entered into the browser as an image and doesn't actually display the correct image.