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...
The Actual connection string is not included in this snippet but is there
And i can supply the test call code if required.
Kind Regards.
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
And i can supply the test call code if required.
Kind Regards.