Question Image into an Access Database problem

Berrilicious

Member
Joined
Apr 14, 2009
Messages
12
Programming Experience
1-3
Good morning,

I have created the following code to allow the user to take a image from the OpenFileDialog and then insert it into my Access database,
This works fine and I have created a test button to retrieve it from the database and display in another PictureBox.

But when i close the application and re open, the image is no longer in the database? This is the first time i have tried to do this so if anyone could shed some light i would be very greatful...

VB.NET:
GetImage.Title = "Select an image"
        GetImage.Filter = "All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg"
        GetImage.FilterIndex = 4

        If GetImage.ShowDialog() = DialogResult.OK Then
            Dim filename As String = GetImage.FileName()
            Try
                oledbcon.Open()

                Dim OleDbInsertCommand As System.Data.OleDb.OleDbCommand = New System.Data.OleDb.OleDbCommand()
                Dim cb As New OleDb.OleDbCommandBuilder()
                OleDbInsertCommand.CommandText = "INSERT INTO CompanyDetails(ID, Logo) VALUES (@ID, @Logo)"
                OleDbInsertCommand.Connection = oledbcon
                OleDbInsertCommand.Parameters.Add(New System.Data.OleDb.OleDbParameter("@ID", System.Data.OleDb.OleDbType.Integer, 4, "ID"))
                OleDbInsertCommand.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Logo", System.Data.OleDb.OleDbType.VarBinary, 2147483647, "Logo"))

                Dim FileStream As System.IO.FileStream
                Dim FileInfo As System.IO.FileInfo

                'READ THE FILE INTO MEMORY
                FileInfo = New System.IO.FileInfo(filename)
                FileStream = New System.IO.FileStream(filename, IO.FileMode.Open)
                Dim ByteArray(CInt(FileInfo.Length - 1)) As Byte
                Debug.WriteLine(FileStream.Read(ByteArray, 0, CInt(FileInfo.Length)))
                FileStream.Close()

                Dim String1 As String
                Dim ASCIIEncoding As New System.Text.ASCIIEncoding()
                String1 = ASCIIEncoding.GetString(ByteArray)

                'RUN THE COMMAND
                OleDbInsertCommand.Parameters("@ID").Value = 1
                OleDbInsertCommand.Parameters("@Logo").Value = ByteArray
                OleDbInsertCommand.ExecuteNonQuery()

            Catch ex As Exception
            Finally
                oledbcon.Close()
            End Try
            companyLogo.Image = Image.FromFile(filename)
            companyLogo.SizeMode = PictureBoxSizeMode.StretchImage
            companyLogo.BorderStyle = BorderStyle.Fixed3D

        End If

The Actual connection string is not included in this snippet but is there :p
And i can supply the test call code if required.

Kind Regards.
 
Highlight the database in solution explorer. In the properties window find the property that says 'copy to output directory'.
 
Thank you this has worked, But now If the user selects a new image, it goes through the process of adding the image to the DB but it outputs the original.

And it doesnt seem to over write the image in the DB.

Regards.
 
Adding pictures to a database is bad enough but to allow updating of those same records will cause your database to grow to enormous sizes unless you compact the database.
 
There will just be one. A company's logo.

For use with a form that needs to be printable.

So it will will allow the user to change the image used on the required printout.

Or is this not a good thing to do?
 
Ok when you update a record in a database table, your essientially creating a whole new record. So say your one record has a 1mb logo picture and you just happen to update the phone number field on that same record, your db size for that record is now at least 2mbs even though you still have only that single 1mb logo attached to it... This happens with all records but is most notable when pictures are attached since they take up much more size then plain text.

I used to run a command to compact the access db at the close of the application that would format the db file and reduce its overall size.
 
Of course ya may not like it but a better method would be to have your program copy the logo/picture file to a specific directory and just record the directorypath & filename to your database record. You then just have to query your db data, read the filename & path from your record and load the file to where you want it.
 
I could do that,
Would it be possible to overwrite the image rather than copy it to a specific directory?
In which case i wouldnt need the DB to store it.

Regards.
 
Not sure exactly what you mean but being possible to overwrite the image... can you clarify this question.
 
I would use a specific file directory in my application and if the user wishs to change the image, they can select a new image and it will overwrite the current image.
 
Umm still not sure what your getting at here, you can set it up to perform however you want it to perform... Thru coding you could store the additional image in the same directory with a new file name or overwrite the existing file. Again its whatever you want it to do.
 
Back
Top