Search code examples
c#attachment-field

C# Attachment field update on existing Access DB


I have an Access table that I recently added an attachment field to. The table is a listing of computers and information about each computer. The users of it, software installed, and so on. The Attachment field will contain a simple text configuration file received from each computer.

I worked with some code found here: Programmatically managing Microsoft Access Attachment-typed field with .NET

My problem is I can't seem to update the correct field. Meaning the configuration file for computer name Bob is getting placed in the correct field but wrong computer if that makes sense. Below is an SQL statement example.

Select * From MyTable WHERE ComputerName Like 'Bob'

Whole code segment below as requested.

public void InsertFile(String Path, String CmpName)
{   
    DBEngine dbe = new DBEngine();
    Database db = dbe.OpenDatabase("MyAccessDB.accdb", false, false, "");
    Recordset rs = db.OpenRecordset(
        "SELECT * FROM MyTable WHERE ComputerName LIKE '" + CmpName + "\'",
        RecordsetTypeEnum.dbOpenDynaset, 0, LockTypeEnum.dbOptimistic);
    rs.MoveFirst();
    rs.Edit();
    Recordset2 rs2 = (Recordset2)rs.Fields["My_File"].Value;
    rs2.AddNew();
    Field2 f2 = (Field2)rs2.Fields["FileData"];
    f2.LoadFromFile(Path);

    rs2._30_Update();
    rs2.Close();
    rs._30_Update();
    rs.Close();          
}

I was hoping that when using OpenRecodeset() the above statement would give me just that row and then the only attachment field for that row. However this is not the case. I have searched to find how to iterate the record set to find the right row with no luck. I am new here so I hope I covered everything I needed to.


Solution

  • I'm not sure if I'm understanding your issue but if all you want is the attachment field your SQL query should be:

    "SELECT attachment FROM MyTable WHERE UCASE(ComputerName) = 'BOB'"