Page 1 of 1

Database and Image display?

Posted: Fri Dec 21, 2012 9:40 am
by hungryhounduk
Hey

I have been working with databases creating a contacts application, Is there any way I can add Images to the database and display an Image everytime i choose a contact in the Database??

Or if i cant do it directly with the database is there another way I can do it with an Image List tied to the Database??

Any Help will be most gratefull and I will give out 1000 CREDITS to anyone who can create a Working example of what i am after and pm me the source, so i can see how its all structured...

Cheers

Chris

Re: Database and Image display?

Posted: Fri Dec 21, 2012 10:45 am
by Shim
i was able to find a working example

This is a simple code snippet which is used to store and retrieve images from Access database using VB.net.
Code: Select all
Private Sub ShowDetails()
    Try
        Dim cn As New OleDb.OleDbConnection
        Dim cmd As OleDb.OleDbCommand
        Dim dr As OleDb.OleDbDataReader

        cn.ConnectionString = mstrConnection
        cn.Open()

        cmd = cn.CreateCommand()
        cmd.CommandText = "SELECT I_Image FROM tblImage WHERE I_Name = '" & cbI_Name.Text & "'"

        dr = cmd.ExecuteReader

        If dr.Read Then
            Dim bytImage() As Byte

            Try
                bytImage = CType(dr(0), Byte())
                Dim ms As New System.IO.MemoryStream(bytImage)
                Dim bmImage As New Bitmap(ms)
                ms.Close()

                pbI_Image.Image = bmImage
                pbI_Image.Refresh()
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End If

        dr.Close()
        cn.Close()

        cmd.Dispose()
        cn.Dispose()
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

Private Sub SaveData()
    Try
        Dim cn As New OleDb.OleDbConnection
        Dim cmd As OleDb.OleDbCommand

        cn.ConnectionString = mstrConnection
        cn.Open()

        cmd = cn.CreateCommand()

        If mstrFlag = "N" Then
            cmd.CommandText = "INSERT INTO tblImage VALUES (@I_Name, @I_Image)"
        ElseIf mstrFlag = "M" Then
            cmd.CommandText = "UPDATE tblImage SET I_Name = @I_Name, I_Image = @I_Image WHERE I_Name = '" & cbI_Name.Tag.ToString & "'"
        End If

        Dim bytImage() As Byte

        Try
            Dim ms As New System.IO.MemoryStream
            Dim bmpImage As New Bitmap(pbI_Image.Image)

            bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
            bytImage = ms.ToArray()
            ms.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

        cmd.Parameters.Add(New OleDb.OleDbParameter("@I_Name", OleDb.OleDbType.VarChar, 120))
        cmd.Parameters.Add(New OleDb.OleDbParameter("@I_Image", OleDb.OleDbType.Binary))
        cmd.Parameters("@I_Name").Value = cbI_Name.Text
        cmd.Parameters("@I_Image").Value = bytImage

        If cmd.ExecuteNonQuery() > 0 Then
            MsgBox("Record has been " & IIf(mstrFlag = "N", "added", "modified").ToString & " successfully.", MsgBoxStyle.Information)
        End If

        cmd.Dispose()
        cn.Dispose()
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

    FillData()
End Sub
source code
ImageDb_src_vb.zip

Re: Database and Image display?

Posted: Fri Dec 21, 2012 12:41 pm
by hungryhounduk
Wow!!!!!

Thanks a lot mshimranpro loove; That is better than i expected it too be, i like the add and save functions.

BRILLIANT

A 1000 CREDITS are being transferd cooll;

You are a Star

respect

Chris