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

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

问题描述

进入加在2007年的版本,新的数据类型 - 附件类型。目前,我们正在与.NET 3.5(C#),使用Access 2007数据库WinForms应用程序。我们希望能够通过的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添加到表中的附件场AttachmentFieldNameTableWithAttachmentField。有一点要注意的是,在尝试两次添加相同的文件将抛出一个错误。

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.

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

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