如何通过.NET将图像插入到Access OLE字段 [英] How to insert an image into an Access OLE field via .NET

查看:262
本文介绍了如何通过.NET将图像插入到Access OLE字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Access .mdb数据库,我想插入一个从Visual C#2010开发的应用程序的图片。图片存储在数据库中OLE对象的字段。

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.

在Access中直接添加图像后,它们以位图图像的格式存储。这些照片可以在Access中通过双击打开。

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.

我有以下代码:

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();

代码运行良好,但Access将图片存储为二进制数据,无法在Access 。请告诉我如何将图像作为位图图像插入。感谢。

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.

推荐答案

这是一个有点不寻常的请求。大多数人在Access中询问关于OLE嵌入图像的问题是要求如何将它们从 OLE对象转换为原始二进制数据,而不是相反。 Access的当前版本具有诸如 Image 控制的功能,其可以显示位图图像,而不必处理将OLE包装器添加到对象数据的复杂性。

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.

仍然,这里是一个方法来做你所要求的。它使用一个 Access.Application 对象,因此Access必须安装在机器上才能正常工作。它还需要Access数据库中的一个Form,其中

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


  • 表单本身绑定到包含要插入的OLE映像字段的表,

  • 表单上唯一的控制项是绑定到OLE字段的绑定对象框架

  • 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.

示例代码还假定要更新的表具有名为[ID]的数字主键字段。

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();
}

这篇关于如何通过.NET将图像插入到Access OLE字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆