Furqan Sehgal
Furqan Sehgal

Reputation: 4997

How to save picture in the database

I am using following code to save my data to database Where does the statement to save a picture in the database fit?

        Dim drNewRowMCQsAns As DataRow
        drNewRowMCQsAns = DsResultSaveNow.tblResult.NewRow

        drNewRowMCQsAns.Item("PaperID") = vrPaperIDInitialized
        drNewRowMCQsAns.Item("StudentID") = vrStudentID
        drNewRowMCQsAns.Item("StudentName") = vrStudentName

        DsResultSaveNow.tblResult.Rows.Add(drNewRowMCQsAns)
        taResultSaveNow.Update(DsResultSaveNow.tblResult)

I have image field in the database but how to save an image? Thanks

Upvotes: 2

Views: 3778

Answers (1)

Robert Beaubien
Robert Beaubien

Reputation: 3156

Well, the image data is simply a byte array

Dim imageData as Byte()

Load your image into the byte array from where ever you are getting it from and set it just like the other properties

drNewRowMCQsAns.Item("ImageData") = imageData

Loading image into array:

From file:

        imageData = IO.File.ReadAllBytes("c:\filename.jpg")

From bitmap:

        Dim bitmap As New System.Drawing.Bitmap("c:\filename.jpg")
    Dim tempMemStream As New IO.MemoryStream
    bitmap.Save(tempMemStream, System.Drawing.Imaging.ImageFormat.Jpeg)
    imageData = tempMemStream.ToArray()

Upvotes: 1

Related Questions