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

查看:22
本文介绍了如何通过 .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 数据库中的表单,其中

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:UsersGordPicturesmpMe.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:UsersPublicDatabase1.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天全站免登陆