Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27514

VS 2008 [RESOLVED] SQL Image save & retrieval not working properly...

$
0
0
Hi guys,

Just recently I've decided to learn how to connect to and manipulate SQL databases, I'm getting on quite well apart from this issue. If I write an image to a table, when I retrieve it I don't get the image. There are no errors and I'm not sure where my problem lies.

OK, the code I'm using is:
Code:

Public Sub store_pic_Sql(ByVal img As Image)
        Dim cs As New SqlConnection("Server=SQLSvr\TestDB;Database=Test;User ID=sa;password=pa55word")
        Dim sql As String = "INSERT INTO images VALUES (@imgData)"
        Dim command1 As SqlCommand = New SqlCommand(sql, cs)
        Dim sqlpara As New SqlParameter("imgData", SqlDbType.Image)
        Dim mStream As MemoryStream = New MemoryStream()
        img.Save(mStream, ImageFormat.Jpeg)
        sqlpara.SqlValue = mStream.GetBuffer
        command1.Parameters.Add(sqlpara)
        cs.Open()
        command1.ExecuteNonQuery()
        cs.Close()
    End Sub

    Public Function Load_Pic_Sql(ByVal id As Integer) As Image
        Dim cs As New SqlConnection("Server=SQLSvr\TestDB;Database=Test;User ID=sa;password=pa55word")
        Dim sql As String = "SELECT image FROM images WHERE iid = " & id
        Dim command1 As SqlCommand = New SqlCommand(sql, cs)
        cs.Open()
        Dim reader As SqlDataReader = command1.ExecuteReader
        reader.Read()
        Dim bitPic() As Byte = CType(reader.GetValue(0), Byte())
        cs.Close()
        Dim mStream As MemoryStream = New MemoryStream(bitPic)
        Dim img As Image = Image.FromStream(mStream)
        Return img
    End Function

    Private Sub OpenFileBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenFileBTN.Click
        Dim Path As String = ""
        Dim OpenXLSFileDialog As New OpenFileDialog()
        Dim blank As Boolean = False
        OpenXLSFileDialog.Filter = "JPG or JPEG Files|*.jpg;*.jpeg"
        OpenXLSFileDialog.Title = "Select Image To Open"
        If OpenXLSFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Path = OpenXLSFileDialog.FileName
            SQLImagePB.ImageLocation = Path
        End If
    End Sub

    Private Sub StoreImageBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StoreImageBTN.Click
        store_pic_Sql(SQLImagePB.InitialImage)
    End Sub

    Private Sub GetSQLImageBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetSQLImageBTN.Click
        SQLImagePB.Image = Load_Pic_Sql(3)  '*Testing* Change this number to open image from a different record...!
    End Sub

For instance, if I open then store an image
Name:  sqlimage1.JPG
Views: 79
Size:  9.2 KB

Then retrieve the image I get this....!
Name:  sqlimage2.JPG
Views: 84
Size:  8.7 KB

Has anyone any idea what is going on or what I've done wrong? I've been sat here looking it it for hours and I'm stumped...!!

Thanks

Dave
Attached Images
  

Viewing all articles
Browse latest Browse all 27514

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>