使用 .NET 以编程方式管理 Microsoft Access 附件类型字段 [英] Programmatically managing Microsoft Access Attachment-typed field with .NET

查看:30
本文介绍了使用 .NET 以编程方式管理 Microsoft Access 附件类型字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Access 在 2007 版本中添加了一种新的数据类型——附件类型.我们目前正在开发一个带有 .NET 3.5 (C#) 的 WinForms 应用程序,该应用程序使用 Access 2007 数据库.我们希望能够通过 WinForms 界面添加新附件.我似乎无法找到有关如何使用 .NET 插入或选择附件数据的任何信息.我确实尝试使用 DAO(版本 12),但它似乎没有在这里讨论 SaveToFile 或 LoadFromFile 方法:http://msdn.microsoft.com/en-us/library/bb258184.aspx

Access added a new data type in the 2007 version--the Attachment type. We are currently working on a WinForms application with .NET 3.5 (C#) that uses an Access 2007 database. We want to be able to add new attachments through the WinForms interface. I can't seem to locate any information on how to insert or select attachment data with .NET. I did try using DAO (version 12) but it didn't seem to have the SaveToFile or LoadFromFile methods discussed here: http://msdn.microsoft.com/en-us/library/bb258184.aspx

那么,我如何使用 .NET 获取附件?

So, how can I get at the attachments with .NET?

推荐答案

我终于使用对 Microsoft.Office.Interop.Access.Dao 的引用在 C# 中实现了这个工作.

I finally got this working in C# using a reference to Microsoft.Office.Interop.Access.Dao.

DBEngine dbe = new DBEngine();
Database db = dbe.OpenDatabase("C:\SomeDatabase.accdb", false, false, "");
Recordset rs = db.OpenRecordset("SELECT * FROM TableWithAttachmentField", RecordsetTypeEnum.dbOpenDynaset, 0, LockTypeEnum.dbOptimistic);
rs.MoveFirst();
rs.Edit();
Recordset2 rs2 = (Recordset2)rs.Fields["AttachmentFieldName"].Value;
rs2.AddNew();

Field2 f2 = (Field2)rs2.Fields["FileData"];

f2.LoadFromFile("C:\test.docx");
rs2._30_Update();
rs2.Close();

rs._30_Update();
rs.Close();

这会将 test.docx 添加到表TableWithAttachmentField"中的附件字段AttachmentFieldName".需要注意的一件事是,尝试添加同一个文件两次会引发错误.

This will add test.docx to the Attachment field "AttachmentFieldName" in table "TableWithAttachmentField". One thing to note is that attempting to add the same file twice will throw an error.

这篇关于使用 .NET 以编程方式管理 Microsoft Access 附件类型字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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