Programmatically managing Microsoft Access Attachment-typed field with .NET

后端 未结 4 839
感动是毒
感动是毒 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条回答
  • Look at this write up on the Access team blog It 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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-09 13:39

    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.

    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.

    0 讨论(0)
  • 2020-12-09 13:47

    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:

    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).

    0 讨论(0)
提交回复
热议问题