Programmatically managing Microsoft Access Attachment-typed field with .NET

后端 未结 4 873
感动是毒
感动是毒 2020-12-09 13:14

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 databas

4条回答
  •  醉酒成梦
    2020-12-09 13:38

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

    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.

提交回复
热议问题