Reading attached files from database using OLE-DB Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Reading attached files from database using OLE-DB without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m trying to read a Microsoft Access Database using C#. I’m using the OLE-DB classes. The Problem is that this code

OleDbDataReader reader = Command.ExecuteReader(); 
while (reader.Read())
    Console.WriteLine(reader.GetFieldType(0) + "t" + reader.GetFieldType(1) + "t" + reader.GetFieldType(2) +
            "t" + reader.GetFieldType(3) + "t" + reader.GetFieldType(4) + "t" + reader.GetFieldType(5));

tells me, that the 5th field is from the datatype string. But it’s an attached file. When I’m trying to read this string, it is empty.

System.Int32    System.String   System.String   System.Int32    System.DateTime    System.String

Is there a way to read attached files from a database?


I realise you asked for OleDb, but with DAO you could say something like:

    DBEngine dbe = new DBEngine();
    Database db = dbe.OpenDatabase(@"z:docstest.accdb", false, false, "");
    Recordset rs = db.OpenRecordset("SELECT TheAttachment FROM TheTable", 
        RecordsetTypeEnum.dbOpenDynaset, 0, LockTypeEnum.dbOptimistic);

    Recordset2 rs2 = (Recordset2)rs.Fields["TheAttachment"].Value;

    Field2 f2 = (Field2)rs2.Fields["FileData"];

Reference: Programmatically managing Microsoft Access Attachment-typed field with .NET

We are here to answer your question about Reading attached files from database using OLE-DB - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji