I have an Access database (@"Provider=Microsoft.ACE.OLEDB.12.0
) which contains a table called FTLH_DBF
. The table is structured as follows:
+----+--------+--------+--------+--------------------------+
| ID | M_TYPE | M_NAME | M_DESC | M_FILE |
+----+--------+--------+--------+--------------------------+
| 1 | 0 | Spot | Blabla | (Attachment: Spot.xml) |
| 2 | 1 | Hedge | Blabla | (Attachment: Hedge.xml) |
| 3 | 2 | Unwind | Blabla | (Attachment: Unwind.xml) |
+----+--------+--------+--------+--------------------------+
By design, the code makes sure that the attachment name M_FILE
is equal to M_NAME
+ .xml
, as well as that no duplicate M_NAME
can be found in the table.
I am trying to read the attachment back after having saved it (successfully, I can see the content of my attachment in the Access' viewer). In order to do this, I've been following this answer: basically I want to read the attachment and save it into a file in the process' folder (that will later be deleted by another function).
This is my method supposed to do this (the input templateName
, according to the table above, would be Spot
, Hedge
, Unwind
...):
public void dumpAttachmentToFile(string templateName)
{
string fileName = templateName + ".xml";
var dbe = new DBEngine();
Microsoft.Office.Interop.Access.Dao.Database db = dbe.OpenDatabase(DB_LOCATION);
Recordset rstMain = db.OpenRecordset(
"select M_FILE from FTLH_DBF where M_NAME = '" + templateName + "';",
RecordsetTypeEnum.dbOpenSnapshot);
Recordset2 rstAttach = rstMain.Fields["M_FILE"].Value;
while ((!fileName.Equals(rstAttach.Fields["M_FILE"].Value)) && (!rstAttach.EOF))
{
rstAttach.MoveNext();
}
if (rstAttach.EOF)
{
Console.WriteLine("Not found.");
}
else
{
Field2 fld = (Field2)rstAttach.Fields["M_FILE"];
fld.SaveToFile(fileName);
}
db.Close();
}
When I execute this code, I get the following runtime error:
System.Runtime.InteropServices.COMException
HResult=0x800A0CC1
Message=Item not found in this collection.
Source=MxML-Factory
StackTrace:
at Microsoft.Office.Interop.Access.Dao.Fields.get_Item(Object Item)
at MxML_Factory.Database.DBConnection.dumpAttachmentToFile(String templateName) in C:\Users\Matteo\source\repos\MxML-Factory\MxML-Factory\Database\DBConnection.cs:line 182
at MxML_Factory.Business.MxML.Load(String m_name) in C:\Users\Matteo\source\repos\MxML-Factory\MxML-Factory\Business\MxML.cs:line 73
at MxML_Factory.Utils.BrowseClassMapper.OpenFormInstance(Object TriggerClass, String loadingKey, DatabaseBrowse callback) in C:\Users\Matteo\source\repos\MxML-Factory\MxML-Factory\Utils\BrowseClassMapper.cs:line 24
at MxML_Factory.WinForms.DatabaseBrowse.OpenInstance(String objectKey) in C:\Users\Matteo\source\repos\MxML-Factory\MxML-Factory\WinForms\DatabaseBrowse.cs:line 66
at MxML_Factory.WinForms.DatabaseBrowse.HandleKeyPress(Object sender, KeyEventArgs e) in C:\Users\Matteo\source\repos\MxML-Factory\MxML-Factory\WinForms\DatabaseBrowse.cs:line 91
[...previous calls of the stack which cannot be guilty...]
... on the following line of code:
while ((!fileName.Equals(rstAttach.Fields["M_FILE"].Value)) && (!rstAttach.EOF))
The object rstAttach
contains a collection Fields
, but all I can see in it is a property count = 6
(can't see the content, and I can't figure out why).
If I open my Access table, I can see there is the attachment I'm looking for (in this case, my fileName
is Spot.xml
:
It seems I'm missing something obvious, but I can't figure out what I'm doing wrong... any tip to investigate further?