简体   繁体   中英

When extracting records from a database using Filehelpers, how do I handle binary fields?

I am using Filehelpers to read data from a database. I have based my code on the example in: http://filehelpers.sourceforge.net/example_sqlstorage_extract.html

My question is, given my code here, how do I handle binary fields?

public class StudentRecord
{
    public string registration_id;
    public string student_number;
    public string card_number;
    public string firstname;
    public string lastname;

    .... // How do I declare the binary data?
    public BinaryData binarydata;

}

....

protected void FillRecordStudent(object rec, object[] fields)
{
    StudentRecord record = (StudentRecord)rec;

    record.registration_id = (string)fields[0];
    record.student_number = (string)fields[1];
    record.card_number = (string)fields[2];
    record.firstname = (string)fields[3];
    record.lastname = (string)fields[4];

    // how do I do this?????
    record.binarydata = (binary data)fields[5];

    ....
}

Any help would be much appreciated.

For the record, i managed to solve this in the following way (I am not sure if its the most elegant solution but it did work for me):

using FileHelpers;
using FileHelpers.DataLink;
using System.Data.Linq;

public class StudentRecord
{
    public string registration_id;
    public string student_number;
    public string card_number;
    public string firstname;
    public string lastname;

    // a binary field
    [FieldConverter(typeof(BinaryConverter))]
    public Binary binarydata;

}


public class BinaryConverter : ConverterBase
{
    public override object StringToField(string from)
    {
        byte[] temparr = Convert.FromBase64String(from);
        Binary res = new Binary(temparr);

        return res;
    }            
}    

....

protected void FillRecordStudent(object rec, object[] fields)
{
    StudentRecord record = (StudentRecord)rec;

    record.registration_id = (string)fields[0];
    record.student_number = (string)fields[1];
    record.card_number = (string)fields[2];
    record.firstname = (string)fields[3];
    record.lastname = (string)fields[4];

    // binary field
    record.binarydata = new Binary((byte[])fields[5]);

    ....
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM