Insert bimap into Access tables using VB.NET

G

Guest

I’m currently facing a problem with inserting a BMP file into an Access table
using VB.NET

When I have executed my program the Image field in my table says “Long
binary dataâ€, here I did expect to see the text “Bitmap picture†and when I
try to click on it I get the following error message (Translated from
Danish): “There was a problem when Microsoft Office Acces was communication
with the OLE server or an Active-X object. Close the OLE server and start it
again outside MS Office. Then try to perform the original task again in MS
officeâ€.

If I import an image into the table from inside Access I get the text
“Bitmap Picture†in the image field and I’m able to double click on the image
field and Access opens the viewer.

Any suggestions?

Here follows my program.


LoadBitmapToImageControl()
CreateAccessDataBase()
Dim DatabaseFullPath As String = "c:\ilse\brev.MDB"

Try
Dim mycnstr As String
Dim str As New MemoryStream
Me.PictureBox1.Image.Save(str, Imaging.ImageFormat.Bmp)

Dim buffer(CInt(str.Length - 1)) As Byte
str.Position = 0
str.Read(buffer, 0, CInt(str.Length))

Dim p As New OleDbParameter
With p
.ParameterName = "@Im"
.OleDbType = OleDbType.Binary
.Value = buffer
End With

mycnstr = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry
Path=;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Database Password=;Data
Source='" & DatabaseFullPath & "';Password=;Jet OLEDB:Engine Type=5;Jet
OLEDB:Global Bulk Transactions=1;Provider='Microsoft.Jet.OLEDB.4.0';Jet
OLEDB:System database=;Jet OLEDB:SFP=False;Extended Properties=;Mode=Share
Deny None;Jet OLEDB:New Database Password=;Jet OLEDB:Create System
Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact
Without Replica Repair=False;User ID=Admin;Jet OLEDB:Encrypt Database=False"

Dim CreateStr As String = "CREATE TABLE letter (ID int Identity(1,1) NOT
NULL, myText String, myImage Image)"

Dim InsertStr1 As String = ""
Dim InsertStr2 As String = "'"
Dim SqlInsertStr As String = ""

SqlInsertStr = "INSERT INTO letter (myImage) values (@Im)"

Dim myConnection As New OleDbConnection(mycnstr)
Dim myCommand As New OleDbCommand(CreateStr, myConnection)

myCommand.Connection.Open()
myCommand.ExecuteNonQuery()
myCommand.Parameters.Clear()
myCommand.Parameters.Add(p)
myCommand.CommandText = SqlInsertStr
myCommand.CommandType = CommandType.Text

myCommand.ExecuteNonQuery()
myConnection.Close()

Catch ex As Exception

MessageBox.Show(ex.Message.ToString)
End Try
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top