By user3260339


2014-02-01 12:19:10 8 Comments

I have an Access .mdb database and I want to insert an image from an application developed in visual C# 2010. Pictures are stored in the database in the field of OLE-object.

After adding images directly in Access they are stored in the format of an Bitmap Image. These pictures can be opened in Access with a double-click.

I have the following code:

OdbcConnection Connection = new OdbcConnection();
...
sql = "INSERT INTO film (poster) VALUES (" ' " + Image.FromFile(textBox8.Text) + " ' ");";
//texbox are stored the picture name
OdbcCommand Command = new OdbcCommand(sql, Connection);
Command.ExecuteNonQuery();

The code works well, but Access stores the picture as binary data and it cannot be opened again in Access. Please tell me how to insert the image as a Bitmap Image. Thanks.

2 comments

@Pintu Singh 2015-04-24 05:24:03

    private string ImageToBase64String(Image image)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            image.Save(stream, image.RawFormat);
            return Convert.ToBase64String(stream.ToArray());
        }
    }

    private void SaveButton()
    {

        string Pic = ImageToBase64String(PicBox.Image);

        OleDbCommand PicSave = new OleDbCommand("INSERT INTO Picture(ID,PICTURE)VALUES(" + PicId.Text + ",'" + Pic + "')", con);
        con.Open();
        var SaveValue = PicSave.ExecuteNonQuery();
        if (SaveValue > 0)
        {
            MessageBox.Show("Record Saved", "Information");
            ValueClear();
        }
        else
            MessageBox.Show("Rocord Not Saved", "Erro Msg");
        con.Close();
    }

@Pintu Singh 2015-04-24 05:25:10

100 % Worked Code For Insert Image Into MS Access Database in OLE Field

@Wai Ha Lee 2015-04-24 05:29:19

Can you explain your code?

@Gord Thompson 2014-02-01 16:27:35

This is a somewhat unusual request. Most people asking about OLE imbedded images in Access are asking about how to convert them from OLE objects into raw binary data, not the other way around. Current versions of Access have features like the Image control that can display bitmap images without having to deal with the complications of OLE "wrappers" being added to the object data.

Still, here is one way to do what you requested. It uses an Access.Application object, so Access must be installed on the machine for this to work. It also requires a Form inside the Access database where

  • the form itself is bound to the table containing the OLE image field you want to insert,
  • the only control on the form is a Bound Object Frame, bound to the OLE field.

PhotoForm.png

The sample code also assumes that the table being updated has a numeric primary key field named [ID].

private void button1_Click(object sender, EventArgs e)
{
    // test data
    int recordIdToUpdate = 15;
    string bmpPath = @"C:\Users\Gord\Pictures\bmpMe.bmp";

    var paths = new System.Collections.Specialized.StringCollection();
    paths.Add(bmpPath);
    Clipboard.SetFileDropList(paths);

    // COM Reference required:
    //     Microsoft Access 14.0 Object Library
    var accApp = new Microsoft.Office.Interop.Access.Application();
    accApp.OpenCurrentDatabase(@"C:\Users\Public\Database1.accdb");
    accApp.DoCmd.OpenForm(
            "PhotoForm",
            Microsoft.Office.Interop.Access.AcFormView.acNormal, 
            null,
            "ID=" + recordIdToUpdate);
    accApp.DoCmd.RunCommand(Microsoft.Office.Interop.Access.AcCommand.acCmdPaste);
    accApp.DoCmd.Close(
            Microsoft.Office.Interop.Access.AcObjectType.acForm, 
            "PhotoForm", 
            Microsoft.Office.Interop.Access.AcCloseSave.acSaveNo);
    accApp.CloseCurrentDatabase();
    accApp.Quit();
    this.Close();
}

@user3260339 2014-02-01 17:47:36

i already have a method to retrieve a picture from ole-filed to the c# program (cutting the ole-header), now i need a reverse method. you suggested a very interesting method, but i need to do this only with C sharp tools, without any changes of db. thank you anyway!

@Gord Thompson 2014-02-01 18:54:21

@user3260339 As you wish. Keep in mind that the Access form does not necessarily have to be in the same database file as the table you are updating. It could be in a separate Access database file with a Linked Table. That would work just as well. (In fact, it would be required if the database file containing the table was a shared back-end in a multiuser environment.)

Related Questions

Sponsored Content

34 Answered Questions

[SOLVED] How to vertically align an image inside a div

26 Answered Questions

[SOLVED] How can I get the application's path in a .NET console application?

31 Answered Questions

[SOLVED] How do I auto-resize an image to fit a 'div' container?

9 Answered Questions

[SOLVED] How to escape braces (curly brackets) in a format string in .NET

1 Answered Questions

[SOLVED] Save Image to Access Database as "Bitmap Image"

  • 2016-08-10 13:42:45
  • DK2014
  • 837 View
  • 1 Score
  • 1 Answer
  • Tags:   vb.net ms-access

1 Answered Questions

[SOLVED] Insert Currency into Access Database C#?

  • 2017-05-30 13:59:07
  • Nouman Arshad
  • 352 View
  • 1 Score
  • 1 Answer
  • Tags:   c# ms-access

1 Answered Questions

[SOLVED] How to Insert C# Image Object into MongoDB

1 Answered Questions

2 Answered Questions

3 Answered Questions

[SOLVED] Insert image into asp.net image tag

Sponsored Content