How to insert an image into an Access OLE field via .NET

前端 未结 2 671
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 00:59

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.

相关标签:
2条回答
  • 2020-12-02 01:38

    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();
    }
    
    0 讨论(0)
  • 2020-12-02 01:46
        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();
        }
    
    0 讨论(0)
提交回复
热议问题