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

查看:146
本文介绍了如何通过.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中使用双击打开。

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.

我有以下的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();

在code运作良好,但访问存储的图片作为二进制数据,它不能在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.

推荐答案

这是一个有点不寻常的请求。大多数人询问OLE嵌入了访问图像询问如何将它们的的OLE对象为原始二进制数据,而不是其他的方式进行转换。访问当前版本有像图片控制,可不必处理的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 对象,所以访问必须在计算机上安装了这个工作。它还需要访问数据库中一个表单,

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.

样品code还假定正在更新的表中有一个名为数字主键字段[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天全站免登陆