Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
588 views
in Technique[技术] by (71.8m points)

c# - Reading attached files from database using OLE-DB

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) + "" + reader.GetFieldType(1) + "" + reader.GetFieldType(2) +
            "" + reader.GetFieldType(3) + "" + reader.GetFieldType(4) + "" + 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

    DBEngine dbe = new DBEngine();
    Database db = dbe.OpenDatabase(@"z:docsest.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"];
    f2.SaveToFile(@"z:docsForExample.xls");
    rs2.Close();
    rs.Close();

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...