vb.net 2005 sql server 2005 storing picture files

Judda

New member
Joined
Aug 8, 2006
Messages
3
Programming Experience
Beginner
Hi there,

I created a DB in SQL server 2005 and one of the columns is an image type. Then using the SQL Server Managment Studio I created an "Insert into" query to push a bunch of text into the text columns and an image (C:\img.jpg) into the image column. The Query appeared to execute and returned no errors, I then spent hours trying to create a VB.Net application that would read the image from the database using a standard SQL connection ("Select * from") , I finally seemed to crack it but all that was returned was "C:\img.jpg" ! Looks like I managed to store away the image path and name instead of the image, although the column does claim to contain <binary>, I suppose that could be used for a text string.
So I need help loading images into a Database, I can find lot's of C# examples, or examples using older versions of VB, but I would love to see how it should be done properly in vb.net code (I am new to it).

Any pointers gratefully received !

Thanks you
Judda
 
Last edited:
I found a really good example in c# as well, so i converted it for you. See the attachment. This is .net 1.1 but there won't be any problems.
 

Attachments

  • Example.zip
    38.4 KB · Views: 61
There is also this article on how to read & write BLOB (image) data in .NET.


-tg
 
Thanks for the sample code, it looks like I was indeed writing images incorrectly to my sql table, as my reader now works !

Well....nearly works......

I seem to be able to dsiplay only a single image, when I know that several should have been read by the Select statement.

I get the feeling I am missing something obvious....

' create SQL command to find photos for connum and date
Dim myCommand As SqlCommand
myCommand = New SqlCommand("Select * from Photos Where [connumber] = '" & connum & "'", conn)
' reader class set up
Dim myReader As SqlDataReader
myReader = myCommand.ExecuteReader
' if no records found in the Photos table then say so
If Not myReader.HasRows Then
'MsgBox("Nothing found")
Label1.Text = "No search results found..."
Label1.Visible = True
End If

Response.ContentType = "image/jpeg"
Do While myReader.Read
Dim imgBuffer As Byte() = CType(myReader.Item("scanphoto"), Byte())
Response.BinaryWrite(imgBuffer)
Loop
conn.Close()
 
Back
Top