Need C# code for reading multiple attachments from Microsoft Access Attachment data type using DataReader
I have multiple documents stored in Attachment data type in Access database. Using DataReader, I need to read multiple attachments along with their file name and store them on the file system. Will appreciate your help...
开发者_高级运维I had a similar problem, here's how I solved it using DAO.
var dbe = new DBEngine();
Database db = dbe.OpenDatabase(@"C:\tmp\access database file.accdb");
try
{
Recordset rstMain = db.OpenRecordset(
"SELECT `Attachment` FROM `table name`",
RecordsetTypeEnum.dbOpenDynaset);
while (!rstMain.EOF)
{
Recordset2 rstAttach = rstMain.Fields["Attachment"].Value;
rstAttach.OpenRecordset();
while (!rstAttach.EOF)
{
Field2 fldAttach = (Field2)rstAttach.Fields["FileData"];
string fileName = rstAttach.Fields["FileName"].Value.ToString();
fldAttach.SaveToFile(@"C:\tmp\" + fileName);
rstAttach.MoveNext();
}
rstAttach.Close();
rstMain.MoveNext();
}
rstMain.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
You should include using Microsoft.Office.Interop.Access.Dao;
in the header.
The only data you get over ADO.NET is semi-colon delimited list of attached files' names. Moreover, you will see that ADO.NET recognizes that column type as String. So, there is no way you would get the actual binary data from the column (on the .NET side).
I would suggest you not using Attachment as data type in Access if you need to access the data from that column outside of Access database. Just create separate table to store all the attachments [links].
精彩评论