C# 使用 .NET 以编程方式管理 Microsoft Access 附件类型字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/779211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:49:48  来源:igfitidea点击:

Programmatically managing Microsoft Access Attachment-typed field with .NET

c#.netms-accessattachment

提问by jamminjulia

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

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

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

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

回答by David-W-Fenton

Interesting question. I don't use A2007, but have the runtime installed, so I used the Access object browser to see what's in there. I discovered something really odd -- there are two FIELD objects, Field and Field2. The attachment functions are members of Field2 but not Field. So, my suggestion would be that perhaps what you need to do is convert this:

有趣的问题。我不使用 A2007,但安装了运行时,所以我使用 Access 对象浏览器来查看其中的内容。我发现了一些非常奇怪的东西——有两个 FIELD 对象,Field 和 Field2。附件函数是 Field2 的成员,但不是 Field 的成员。所以,我的建议是,也许你需要做的是转换这个:

Recordset.Fields("FileData").LoadFromFile(<filename>)

to something like this:

像这样:

Dim rs As DAO.Recordset
Dim fld2 As DAO.Field2

Set rs = CurrentDb.OpenRecordset("[SQL]")
Set fld2 = Recordset.Fields("FileData")
fld2.LoadFromFile(<filename>)

rs.Close
Set fld2=Nothing

Now, I don't know if that will correct the problem for you, but it seems to me that given the two Field objects with different properties/methods/members, you need to be explicit about which Field object you're using. The code example you cite is specifically for use in Access and maybe Access does something to automatically resolve the differences between the two object (perhaps it uses the Field object by default for non-ACCDB databases and the Field2 object for ACCDB files).

现在,我不知道这是否会为您解决问题,但在我看来,鉴于具有不同属性/方法/成员的两个 Field 对象,您需要明确说明您正在使用哪个 Field 对象。您引用的代码示例专门用于 Access,也许 Access 会自动解决两个对象之间的差异(也许它默认使用 Field 对象用于非 ACCDB 数据库和 Field2 对象用于 ACCDB 文件)。

回答by Steve Mallory

Look at this write up on the Access team blogIt has basically what David is suggesting with a bit of a twist. It sets a Recordset2 type object equal to the value of the attachment field. Then appends a record to that recordset a puts the file contents in that new record.

看看Access 团队博客上的这篇文章,它基本上包含了 David 建议的内容,但略有不同。它将 Recordset2 类型对象设置为等于附件字段的值。然后向该记录集追加一条记录,并将文件内容放入该新记录中。

回答by Adam

I've been struggling to try and do this same thing. There is a reference you can include in your project to "Microsoft.Office.Interop.Access.Dao" that will get you the Recordset2 and Field2 Interfaces, but no implementation classes.

我一直在努力尝试做同样的事情。您可以在您的项目中包含对“Microsoft.Office.Interop.Access.Dao”的引用,该引用将为您提供 Recordset2 和 Field2 接口,但没有实现类。

That's about as far as I've gotten, I'll post more once/if I figure it out...

就我所知,我会再发一次/如果我弄明白了......

I was not able to get this working in C#, so I moved on to a different solution. I would love to know how to do this though if someone figures it out.

我无法在 C# 中实现它,所以我转向了不同的解决方案。如果有人弄清楚了,我很想知道如何做到这一点。

回答by Nick Sarabyn

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

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

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

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.

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